blob: ff9ee313200d961af0220fdd5e0b286181346031 [file] [log] [blame]
Gilles Debunne60e3b3f2012-03-13 11:26:05 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
Raph Levien39b4db72015-03-25 13:18:20 -070019import android.annotation.IntDef;
Siyamed Sinir0fa89d62017-07-24 20:46:41 -070020import android.annotation.IntRange;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
Doug Felt9f7a4442010-03-01 12:45:56 -080023import android.graphics.Path;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.text.method.TextKeyListener;
Doug Felt9f7a4442010-03-01 12:45:56 -080026import android.text.style.AlignmentSpan;
27import android.text.style.LeadingMarginSpan;
Gilles Debunne162bf0f2010-11-16 16:23:53 -080028import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
Doug Felt9f7a4442010-03-01 12:45:56 -080029import android.text.style.LineBackgroundSpan;
30import android.text.style.ParagraphStyle;
31import android.text.style.ReplacementSpan;
32import android.text.style.TabStopSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Siyamed Sinired09ae12016-02-16 14:36:26 -080034import com.android.internal.annotations.VisibleForTesting;
Doug Feltcb3791202011-07-07 11:57:48 -070035import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050036import com.android.internal.util.GrowingArrayUtils;
Doug Feltcb3791202011-07-07 11:57:48 -070037
Raph Levien39b4db72015-03-25 13:18:20 -070038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Doug Feltc982f602010-05-25 11:51:40 -070040import java.util.Arrays;
Doug Felt9f7a4442010-03-01 12:45:56 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
Doug Felt9f7a4442010-03-01 12:45:56 -080043 * A base class that manages text layout in visual elements on
44 * the screen.
45 * <p>For text that will be edited, use a {@link DynamicLayout},
46 * which will be updated as the text changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * For text that will not change, use a {@link StaticLayout}.
48 */
49public abstract class Layout {
Raph Levien39b4db72015-03-25 13:18:20 -070050 /** @hide */
51 @IntDef({BREAK_STRATEGY_SIMPLE, BREAK_STRATEGY_HIGH_QUALITY, BREAK_STRATEGY_BALANCED})
52 @Retention(RetentionPolicy.SOURCE)
53 public @interface BreakStrategy {}
54
55 /**
56 * Value for break strategy indicating simple line breaking. Automatic hyphens are not added
57 * (though soft hyphens are respected), and modifying text generally doesn't affect the layout
58 * before it (which yields a more consistent user experience when editing), but layout may not
59 * be the highest quality.
60 */
61 public static final int BREAK_STRATEGY_SIMPLE = 0;
62
63 /**
64 * Value for break strategy indicating high quality line breaking, including automatic
65 * hyphenation and doing whole-paragraph optimization of line breaks.
66 */
67 public static final int BREAK_STRATEGY_HIGH_QUALITY = 1;
68
69 /**
70 * Value for break strategy indicating balanced line breaking. The breaks are chosen to
71 * make all lines as close to the same length as possible, including automatic hyphenation.
72 */
73 public static final int BREAK_STRATEGY_BALANCED = 2;
74
Roozbeh Pournader95c7a132015-05-12 12:01:06 -070075 /** @hide */
76 @IntDef({HYPHENATION_FREQUENCY_NORMAL, HYPHENATION_FREQUENCY_FULL,
77 HYPHENATION_FREQUENCY_NONE})
78 @Retention(RetentionPolicy.SOURCE)
79 public @interface HyphenationFrequency {}
80
81 /**
82 * Value for hyphenation frequency indicating no automatic hyphenation. Useful
83 * for backward compatibility, and for cases where the automatic hyphenation algorithm results
84 * in incorrect hyphenation. Mid-word breaks may still happen when a word is wider than the
85 * layout and there is otherwise no valid break. Soft hyphens are ignored and will not be used
86 * as suggestions for potential line breaks.
87 */
88 public static final int HYPHENATION_FREQUENCY_NONE = 0;
89
90 /**
91 * Value for hyphenation frequency indicating a light amount of automatic hyphenation, which
92 * is a conservative default. Useful for informal cases, such as short sentences or chat
93 * messages.
94 */
95 public static final int HYPHENATION_FREQUENCY_NORMAL = 1;
96
97 /**
98 * Value for hyphenation frequency indicating the full amount of automatic hyphenation, typical
99 * in typography. Useful for running text and where it's important to put the maximum amount of
100 * text in a screen with limited space.
101 */
102 public static final int HYPHENATION_FREQUENCY_FULL = 2;
103
Doug Felt71b8dd72010-02-16 17:27:09 -0800104 private static final ParagraphStyle[] NO_PARA_SPANS =
105 ArrayUtils.emptyArray(ParagraphStyle.class);
Dave Bort76c02262009-04-13 17:17:21 -0700106
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700107 /** @hide */
108 @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD})
109 @Retention(RetentionPolicy.SOURCE)
110 public @interface JustificationMode {}
111
112 /**
113 * Value for justification mode indicating no justification.
114 */
115 public static final int JUSTIFICATION_MODE_NONE = 0;
116
117 /**
118 * Value for justification mode indicating the text is justified by stretching word spacing.
119 */
120 public static final int JUSTIFICATION_MODE_INTER_WORD = 1;
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700123 * Return how wide a layout must be in order to display the specified text with one line per
124 * paragraph.
125 *
126 * <p>As of O, Uses
127 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
128 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
130 public static float getDesiredWidth(CharSequence source,
131 TextPaint paint) {
132 return getDesiredWidth(source, 0, source.length(), paint);
133 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700136 * Return how wide a layout must be in order to display the specified text slice with one
137 * line per paragraph.
138 *
139 * <p>As of O, Uses
140 * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
141 * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
142 */
143 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
144 return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
145 }
146
147 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800148 * Return how wide a layout must be in order to display the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * specified text slice with one line per paragraph.
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700150 *
151 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700153 public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
154 TextDirectionHeuristic textDir) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 float need = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 int next;
158 for (int i = start; i <= end; i = next) {
159 next = TextUtils.indexOf(source, '\n', i, end);
160
161 if (next < 0)
162 next = end;
163
Doug Felt71b8dd72010-02-16 17:27:09 -0800164 // note, omits trailing paragraph char
Siyamed Sinir79bf9d12016-05-18 19:57:52 -0700165 float w = measurePara(paint, source, i, next, textDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167 if (w > need)
168 need = w;
169
170 next++;
171 }
172
173 return need;
174 }
175
176 /**
177 * Subclasses of Layout use this constructor to set the display text,
178 * width, and other standard properties.
Doug Felt71b8dd72010-02-16 17:27:09 -0800179 * @param text the text to render
180 * @param paint the default paint for the layout. Styles can override
181 * various attributes of the paint.
182 * @param width the wrapping width for the text.
183 * @param align whether to left, right, or center the text. Styles can
184 * override the alignment.
185 * @param spacingMult factor by which to scale the font size to get the
186 * default line spacing
187 * @param spacingAdd amount to add to the default line spacing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
189 protected Layout(CharSequence text, TextPaint paint,
190 int width, Alignment align,
Doug Felt71b8dd72010-02-16 17:27:09 -0800191 float spacingMult, float spacingAdd) {
Doug Feltcb3791202011-07-07 11:57:48 -0700192 this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR,
193 spacingMult, spacingAdd);
194 }
195
196 /**
197 * Subclasses of Layout use this constructor to set the display text,
198 * width, and other standard properties.
199 * @param text the text to render
200 * @param paint the default paint for the layout. Styles can override
201 * various attributes of the paint.
202 * @param width the wrapping width for the text.
203 * @param align whether to left, right, or center the text. Styles can
204 * override the alignment.
205 * @param spacingMult factor by which to scale the font size to get the
206 * default line spacing
207 * @param spacingAdd amount to add to the default line spacing
208 *
209 * @hide
210 */
211 protected Layout(CharSequence text, TextPaint paint,
212 int width, Alignment align, TextDirectionHeuristic textDir,
213 float spacingMult, float spacingAdd) {
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 if (width < 0)
216 throw new IllegalArgumentException("Layout: " + width + " < 0");
217
Doug Felte8e45f22010-03-29 14:58:40 -0700218 // Ensure paint doesn't have baselineShift set.
219 // While normally we don't modify the paint the user passed in,
220 // we were already doing this in Styled.drawUniformRun with both
221 // baselineShift and bgColor. We probably should reevaluate bgColor.
222 if (paint != null) {
223 paint.bgColor = 0;
224 paint.baselineShift = 0;
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 mText = text;
228 mPaint = paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 mWidth = width;
230 mAlignment = align;
Doug Felt71b8dd72010-02-16 17:27:09 -0800231 mSpacingMult = spacingMult;
232 mSpacingAdd = spacingAdd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 mSpannedText = text instanceof Spanned;
Doug Feltcb3791202011-07-07 11:57:48 -0700234 mTextDir = textDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900237 /** @hide */
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700238 protected void setJustificationMode(@JustificationMode int justificationMode) {
239 mJustificationMode = justificationMode;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900240 }
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * Replace constructor properties of this Layout with new ones. Be careful.
244 */
245 /* package */ void replaceWith(CharSequence text, TextPaint paint,
246 int width, Alignment align,
247 float spacingmult, float spacingadd) {
248 if (width < 0) {
249 throw new IllegalArgumentException("Layout: " + width + " < 0");
250 }
251
252 mText = text;
253 mPaint = paint;
254 mWidth = width;
255 mAlignment = align;
256 mSpacingMult = spacingmult;
257 mSpacingAdd = spacingadd;
258 mSpannedText = text instanceof Spanned;
259 }
260
261 /**
262 * Draw this Layout on the specified Canvas.
263 */
264 public void draw(Canvas c) {
265 draw(c, null, null, 0);
266 }
267
268 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800269 * Draw this Layout on the specified canvas, with the highlight path drawn
270 * between the background and the text.
271 *
Gilles Debunne6c488de2012-03-01 16:20:35 -0800272 * @param canvas the canvas
Doug Felt71b8dd72010-02-16 17:27:09 -0800273 * @param highlight the path of the highlight or cursor; can be null
274 * @param highlightPaint the paint for the highlight
275 * @param cursorOffsetVertical the amount to temporarily translate the
276 * canvas while rendering the highlight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Gilles Debunne6c488de2012-03-01 16:20:35 -0800278 public void draw(Canvas canvas, Path highlight, Paint highlightPaint,
279 int cursorOffsetVertical) {
280 final long lineRange = getLineRangeForDraw(canvas);
281 int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
282 int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
283 if (lastLine < 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
Gilles Debunne6c488de2012-03-01 16:20:35 -0800285 drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
286 firstLine, lastLine);
287 drawText(canvas, firstLine, lastLine);
288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900290 private boolean isJustificationRequired(int lineNum) {
Seigo Nonaka4b4730d2017-03-31 09:42:16 -0700291 if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900292 final int lineEnd = getLineEnd(lineNum);
293 return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
294 }
295
296 private float getJustifyWidth(int lineNum) {
297 Alignment paraAlign = mAlignment;
298 TabStops tabStops = null;
299 boolean tabStopsIsInitialized = false;
300
301 int left = 0;
302 int right = mWidth;
303
304 final int dir = getParagraphDirection(lineNum);
305
306 ParagraphStyle[] spans = NO_PARA_SPANS;
307 if (mSpannedText) {
308 Spanned sp = (Spanned) mText;
309 final int start = getLineStart(lineNum);
310
311 final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
312
313 if (isFirstParaLine) {
314 final int spanEnd = sp.nextSpanTransition(start, mText.length(),
315 ParagraphStyle.class);
316 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
317
318 for (int n = spans.length - 1; n >= 0; n--) {
319 if (spans[n] instanceof AlignmentSpan) {
320 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
321 break;
322 }
323 }
324 }
325
326 final int length = spans.length;
327 boolean useFirstLineMargin = isFirstParaLine;
328 for (int n = 0; n < length; n++) {
329 if (spans[n] instanceof LeadingMarginSpan2) {
330 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
331 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
332 if (lineNum < startLine + count) {
333 useFirstLineMargin = true;
334 break;
335 }
336 }
337 }
338 for (int n = 0; n < length; n++) {
339 if (spans[n] instanceof LeadingMarginSpan) {
340 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
341 if (dir == DIR_RIGHT_TO_LEFT) {
342 right -= margin.getLeadingMargin(useFirstLineMargin);
343 } else {
344 left += margin.getLeadingMargin(useFirstLineMargin);
345 }
346 }
347 }
348 }
349
350 if (getLineContainsTab(lineNum)) {
351 tabStops = new TabStops(TAB_INCREMENT, spans);
352 }
353
354 final Alignment align;
355 if (paraAlign == Alignment.ALIGN_LEFT) {
356 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
357 } else if (paraAlign == Alignment.ALIGN_RIGHT) {
358 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
359 } else {
360 align = paraAlign;
361 }
362
363 final int indentWidth;
364 if (align == Alignment.ALIGN_NORMAL) {
365 if (dir == DIR_LEFT_TO_RIGHT) {
366 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
367 } else {
368 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
369 }
370 } else if (align == Alignment.ALIGN_OPPOSITE) {
371 if (dir == DIR_LEFT_TO_RIGHT) {
372 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
373 } else {
374 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
375 }
376 } else { // Alignment.ALIGN_CENTER
377 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
378 }
379
380 return right - left - indentWidth;
381 }
382
Gilles Debunne6c488de2012-03-01 16:20:35 -0800383 /**
384 * @hide
385 */
386 public void drawText(Canvas canvas, int firstLine, int lastLine) {
387 int previousLineBottom = getLineTop(firstLine);
388 int previousLineEnd = getLineStart(firstLine);
Doug Felt71b8dd72010-02-16 17:27:09 -0800389 ParagraphStyle[] spans = NO_PARA_SPANS;
Doug Feltc982f602010-05-25 11:51:40 -0700390 int spanEnd = 0;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900391 final TextPaint paint = mPaint;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800392 CharSequence buf = mText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700394 Alignment paraAlign = mAlignment;
Doug Feltc982f602010-05-25 11:51:40 -0700395 TabStops tabStops = null;
396 boolean tabStopsIsInitialized = false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800397
Doug Felte8e45f22010-03-29 14:58:40 -0700398 TextLine tl = TextLine.obtain();
Doug Feltc982f602010-05-25 11:51:40 -0700399
Gilles Debunne6c488de2012-03-01 16:20:35 -0800400 // Draw the lines, one at a time.
401 // The baseline is the top of the following line minus the current line's descent.
Raph Levien26d443a2015-03-30 14:18:32 -0700402 for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 int start = previousLineEnd;
Raph Levien26d443a2015-03-30 14:18:32 -0700404 previousLineEnd = getLineStart(lineNum + 1);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900405 final boolean justify = isJustificationRequired(lineNum);
Raph Levien26d443a2015-03-30 14:18:32 -0700406 int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
408 int ltop = previousLineBottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700409 int lbottom = getLineTop(lineNum + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 previousLineBottom = lbottom;
Raph Levien26d443a2015-03-30 14:18:32 -0700411 int lbaseline = lbottom - getLineDescent(lineNum);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Raph Levien26d443a2015-03-30 14:18:32 -0700413 int dir = getParagraphDirection(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700414 int left = 0;
415 int right = mWidth;
416
Gilles Debunne6c488de2012-03-01 16:20:35 -0800417 if (mSpannedText) {
Doug Feltc982f602010-05-25 11:51:40 -0700418 Spanned sp = (Spanned) buf;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800419 int textLength = buf.length();
420 boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
Doug Felt0c702b82010-05-14 10:55:42 -0700421
Doug Feltc982f602010-05-25 11:51:40 -0700422 // New batch of paragraph styles, collect into spans array.
423 // Compute the alignment, last alignment style wins.
424 // Reset tabStops, we'll rebuild if we encounter a line with
425 // tabs.
426 // We expect paragraph spans to be relatively infrequent, use
427 // spanEnd so that we can check less frequently. Since
428 // paragraph styles ought to apply to entire paragraphs, we can
429 // just collect the ones present at the start of the paragraph.
430 // If spanEnd is before the end of the paragraph, that's not
431 // our problem.
Raph Levien26d443a2015-03-30 14:18:32 -0700432 if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
Doug Feltc982f602010-05-25 11:51:40 -0700433 spanEnd = sp.nextSpanTransition(start, textLength,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 ParagraphStyle.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -0700435 spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
Doug Felt9f7a4442010-03-01 12:45:56 -0800436
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700437 paraAlign = mAlignment;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800438 for (int n = spans.length - 1; n >= 0; n--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 if (spans[n] instanceof AlignmentSpan) {
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700440 paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 break;
442 }
443 }
Doug Felt0c702b82010-05-14 10:55:42 -0700444
Doug Feltc982f602010-05-25 11:51:40 -0700445 tabStopsIsInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800447
Doug Feltc982f602010-05-25 11:51:40 -0700448 // Draw all leading margin spans. Adjust left or right according
449 // to the paragraph direction of the line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 final int length = spans.length;
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700451 boolean useFirstLineMargin = isFirstParaLine;
452 for (int n = 0; n < length; n++) {
453 if (spans[n] instanceof LeadingMarginSpan2) {
454 int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
455 int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
456 // if there is more than one LeadingMarginSpan2, use
457 // the count that is greatest
Raph Levien26d443a2015-03-30 14:18:32 -0700458 if (lineNum < startLine + count) {
Anish Athalyeab08c6d2014-08-08 12:09:58 -0700459 useFirstLineMargin = true;
460 break;
461 }
462 }
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 for (int n = 0; n < length; n++) {
465 if (spans[n] instanceof LeadingMarginSpan) {
466 LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 if (dir == DIR_RIGHT_TO_LEFT) {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800468 margin.drawLeadingMargin(canvas, paint, right, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800470 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700471 right -= margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 } else {
Gilles Debunne6c488de2012-03-01 16:20:35 -0800473 margin.drawLeadingMargin(canvas, paint, left, dir, ltop,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 lbaseline, lbottom, buf,
Doug Felt71b8dd72010-02-16 17:27:09 -0800475 start, end, isFirstParaLine, this);
Doug Feltc982f602010-05-25 11:51:40 -0700476 left += margin.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
479 }
480 }
481
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700482 boolean hasTab = getLineContainsTab(lineNum);
Doug Feltc982f602010-05-25 11:51:40 -0700483 // Can't tell if we have tabs for sure, currently
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700484 if (hasTab && !tabStopsIsInitialized) {
Doug Feltc982f602010-05-25 11:51:40 -0700485 if (tabStops == null) {
486 tabStops = new TabStops(TAB_INCREMENT, spans);
487 } else {
488 tabStops.reset(TAB_INCREMENT, spans);
489 }
490 tabStopsIsInitialized = true;
491 }
492
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700493 // Determine whether the line aligns to normal, opposite, or center.
494 Alignment align = paraAlign;
495 if (align == Alignment.ALIGN_LEFT) {
496 align = (dir == DIR_LEFT_TO_RIGHT) ?
497 Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
498 } else if (align == Alignment.ALIGN_RIGHT) {
499 align = (dir == DIR_LEFT_TO_RIGHT) ?
500 Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
501 }
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 int x;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900504 final int indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 if (align == Alignment.ALIGN_NORMAL) {
506 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900507 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
508 x = left + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900510 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
511 x = right - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513 } else {
Raph Levien26d443a2015-03-30 14:18:32 -0700514 int max = (int)getLineExtent(lineNum, tabStops, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700516 if (dir == DIR_LEFT_TO_RIGHT) {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900517 indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
518 x = right - max - indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 } else {
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900520 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
521 x = left - max + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
Doug Feltc982f602010-05-25 11:51:40 -0700523 } else { // Alignment.ALIGN_CENTER
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900524 indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700525 max = max & ~1;
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900526 x = ((right + left - max) >> 1) + indentWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 }
529
Raph Levien26d443a2015-03-30 14:18:32 -0700530 paint.setHyphenEdit(getHyphen(lineNum));
531 Directions directions = getLineDirections(lineNum);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900532 if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
Doug Felt71b8dd72010-02-16 17:27:09 -0800533 // XXX: assumes there's nothing additional to be done
Gilles Debunne6c488de2012-03-01 16:20:35 -0800534 canvas.drawText(buf, start, end, x, lbaseline, paint);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 } else {
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700536 tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +0900537 if (justify) {
538 tl.justify(right - left - indentWidth);
539 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800540 tl.draw(canvas, x, ltop, lbaseline, lbottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
Raph Levien9a174dd2015-04-08 13:35:03 -0700542 paint.setHyphenEdit(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
Doug Feltc982f602010-05-25 11:51:40 -0700544
Doug Felte8e45f22010-03-29 14:58:40 -0700545 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547
548 /**
Gilles Debunne6c488de2012-03-01 16:20:35 -0800549 * @hide
550 */
551 public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
552 int cursorOffsetVertical, int firstLine, int lastLine) {
553 // First, draw LineBackgroundSpans.
554 // LineBackgroundSpans know nothing about the alignment, margins, or
555 // direction of the layout or line. XXX: Should they?
556 // They are evaluated at each line.
557 if (mSpannedText) {
Gilles Debunneeca5b732012-04-25 18:48:42 -0700558 if (mLineBackgroundSpans == null) {
559 mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700560 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800561
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700562 Spanned buffer = (Spanned) mText;
563 int textLength = buffer.length();
Gilles Debunneeca5b732012-04-25 18:48:42 -0700564 mLineBackgroundSpans.init(buffer, 0, textLength);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800565
Gilles Debunneeca5b732012-04-25 18:48:42 -0700566 if (mLineBackgroundSpans.numberOfSpans > 0) {
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700567 int previousLineBottom = getLineTop(firstLine);
568 int previousLineEnd = getLineStart(firstLine);
569 ParagraphStyle[] spans = NO_PARA_SPANS;
570 int spansLength = 0;
571 TextPaint paint = mPaint;
572 int spanEnd = 0;
573 final int width = mWidth;
574 for (int i = firstLine; i <= lastLine; i++) {
575 int start = previousLineEnd;
576 int end = getLineStart(i + 1);
577 previousLineEnd = end;
Gilles Debunne6c488de2012-03-01 16:20:35 -0800578
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700579 int ltop = previousLineBottom;
580 int lbottom = getLineTop(i + 1);
581 previousLineBottom = lbottom;
582 int lbaseline = lbottom - getLineDescent(i);
583
584 if (start >= spanEnd) {
585 // These should be infrequent, so we'll use this so that
586 // we don't have to check as often.
Gilles Debunneeca5b732012-04-25 18:48:42 -0700587 spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700588 // All LineBackgroundSpans on a line contribute to its background.
589 spansLength = 0;
590 // Duplication of the logic of getParagraphSpans
591 if (start != end || start == 0) {
592 // Equivalent to a getSpans(start, end), but filling the 'spans' local
593 // array instead to reduce memory allocation
Gilles Debunneeca5b732012-04-25 18:48:42 -0700594 for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
595 // equal test is valid since both intervals are not empty by
596 // construction
597 if (mLineBackgroundSpans.spanStarts[j] >= end ||
598 mLineBackgroundSpans.spanEnds[j] <= start) continue;
Adam Lesinski776abc22014-03-07 11:30:59 -0500599 spans = GrowingArrayUtils.append(
600 spans, spansLength, mLineBackgroundSpans.spans[j]);
601 spansLength++;
Gilles Debunne60e3b3f2012-03-13 11:26:05 -0700602 }
603 }
604 }
605
606 for (int n = 0; n < spansLength; n++) {
607 LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
608 lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
609 ltop, lbaseline, lbottom,
610 buffer, start, end, i);
611 }
Gilles Debunne6c488de2012-03-01 16:20:35 -0800612 }
613 }
Gilles Debunneeca5b732012-04-25 18:48:42 -0700614 mLineBackgroundSpans.recycle();
Gilles Debunne6c488de2012-03-01 16:20:35 -0800615 }
616
617 // There can be a highlight even without spans if we are drawing
618 // a non-spanned transformation of a spanned editing buffer.
619 if (highlight != null) {
620 if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
621 canvas.drawPath(highlight, highlightPaint);
622 if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
623 }
624 }
625
626 /**
627 * @param canvas
628 * @return The range of lines that need to be drawn, possibly empty.
629 * @hide
630 */
631 public long getLineRangeForDraw(Canvas canvas) {
632 int dtop, dbottom;
633
634 synchronized (sTempRect) {
635 if (!canvas.getClipBounds(sTempRect)) {
636 // Negative range end used as a special flag
637 return TextUtils.packRangeInLong(0, -1);
638 }
639
640 dtop = sTempRect.top;
641 dbottom = sTempRect.bottom;
642 }
643
644 final int top = Math.max(dtop, 0);
645 final int bottom = Math.min(getLineTop(getLineCount()), dbottom);
646
Gilles Debunne2fba3382012-06-11 17:46:24 -0700647 if (top >= bottom) return TextUtils.packRangeInLong(0, -1);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800648 return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom));
649 }
650
651 /**
Doug Feltc982f602010-05-25 11:51:40 -0700652 * Return the start position of the line, given the left and right bounds
653 * of the margins.
Doug Felt0c702b82010-05-14 10:55:42 -0700654 *
Doug Feltc982f602010-05-25 11:51:40 -0700655 * @param line the line index
656 * @param left the left bounds (0, or leading margin if ltr para)
657 * @param right the right bounds (width, minus leading margin if rtl para)
658 * @return the start position of the line (to right of line if rtl para)
659 */
660 private int getLineStartPos(int line, int left, int right) {
661 // Adjust the point at which to start rendering depending on the
662 // alignment of the paragraph.
663 Alignment align = getParagraphAlignment(line);
664 int dir = getParagraphDirection(line);
665
Doug Feltc0ccf0c2011-06-23 16:13:18 -0700666 if (align == Alignment.ALIGN_LEFT) {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700667 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
668 } else if (align == Alignment.ALIGN_RIGHT) {
669 align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
670 }
671
672 int x;
673 if (align == Alignment.ALIGN_NORMAL) {
Doug Feltc982f602010-05-25 11:51:40 -0700674 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700675 x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700676 } else {
Raph Levien2ea52902015-07-01 14:39:31 -0700677 x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700678 }
679 } else {
680 TabStops tabStops = null;
681 if (mSpannedText && getLineContainsTab(line)) {
682 Spanned spanned = (Spanned) mText;
683 int start = getLineStart(line);
684 int spanEnd = spanned.nextSpanTransition(start, spanned.length(),
685 TabStopSpan.class);
Gilles Debunne6c488de2012-03-01 16:20:35 -0800686 TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd,
687 TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -0700688 if (tabSpans.length > 0) {
689 tabStops = new TabStops(TAB_INCREMENT, tabSpans);
690 }
691 }
692 int max = (int)getLineExtent(line, tabStops, false);
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700693 if (align == Alignment.ALIGN_OPPOSITE) {
Doug Feltc982f602010-05-25 11:51:40 -0700694 if (dir == DIR_LEFT_TO_RIGHT) {
Raph Levien2ea52902015-07-01 14:39:31 -0700695 x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT);
Doug Feltc982f602010-05-25 11:51:40 -0700696 } else {
Fabrice Di Megliodb24f0a2012-10-03 17:17:14 -0700697 // max is negative here
Raph Levien2ea52902015-07-01 14:39:31 -0700698 x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT);
Doug Feltc982f602010-05-25 11:51:40 -0700699 }
700 } else { // Alignment.ALIGN_CENTER
701 max = max & ~1;
Raph Levien2ea52902015-07-01 14:39:31 -0700702 x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER);
Doug Feltc982f602010-05-25 11:51:40 -0700703 }
704 }
705 return x;
706 }
707
708 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 * Return the text that is displayed by this Layout.
710 */
711 public final CharSequence getText() {
712 return mText;
713 }
714
715 /**
716 * Return the base Paint properties for this layout.
717 * Do NOT change the paint, which may result in funny
718 * drawing for this layout.
719 */
720 public final TextPaint getPaint() {
721 return mPaint;
722 }
723
724 /**
725 * Return the width of this layout.
726 */
727 public final int getWidth() {
728 return mWidth;
729 }
730
731 /**
732 * Return the width to which this Layout is ellipsizing, or
733 * {@link #getWidth} if it is not doing anything special.
734 */
735 public int getEllipsizedWidth() {
736 return mWidth;
737 }
738
739 /**
740 * Increase the width of this layout to the specified width.
Doug Felt71b8dd72010-02-16 17:27:09 -0800741 * Be careful to use this only when you know it is appropriate&mdash;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 * it does not cause the text to reflow to use the full new width.
743 */
744 public final void increaseWidthTo(int wid) {
745 if (wid < mWidth) {
746 throw new RuntimeException("attempted to reduce Layout width");
747 }
748
749 mWidth = wid;
750 }
Doug Felt9f7a4442010-03-01 12:45:56 -0800751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 /**
753 * Return the total height of this layout.
754 */
755 public int getHeight() {
Doug Felt71b8dd72010-02-16 17:27:09 -0800756 return getLineTop(getLineCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
759 /**
Siyamed Sinir0745c722016-05-31 20:39:33 -0700760 * Return the total height of this layout.
761 *
762 * @param cap if true and max lines is set, returns the height of the layout at the max lines.
763 *
764 * @hide
765 */
766 public int getHeight(boolean cap) {
767 return getHeight();
768 }
769
770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 * Return the base alignment of this layout.
772 */
773 public final Alignment getAlignment() {
774 return mAlignment;
775 }
776
777 /**
778 * Return what the text height is multiplied by to get the line height.
779 */
780 public final float getSpacingMultiplier() {
781 return mSpacingMult;
782 }
783
784 /**
785 * Return the number of units of leading that are added to each line.
786 */
787 public final float getSpacingAdd() {
788 return mSpacingAdd;
789 }
790
791 /**
Doug Feltcb3791202011-07-07 11:57:48 -0700792 * Return the heuristic used to determine paragraph text direction.
793 * @hide
794 */
795 public final TextDirectionHeuristic getTextDirectionHeuristic() {
796 return mTextDir;
797 }
798
799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 * Return the number of lines of text in this layout.
801 */
802 public abstract int getLineCount();
Doug Felt9f7a4442010-03-01 12:45:56 -0800803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 /**
805 * Return the baseline for the specified line (0&hellip;getLineCount() - 1)
806 * If bounds is not null, return the top, left, right, bottom extents
807 * of the specified line in it.
808 * @param line which line to examine (0..getLineCount() - 1)
809 * @param bounds Optional. If not null, it returns the extent of the line
810 * @return the Y-coordinate of the baseline
811 */
812 public int getLineBounds(int line, Rect bounds) {
813 if (bounds != null) {
814 bounds.left = 0; // ???
815 bounds.top = getLineTop(line);
816 bounds.right = mWidth; // ???
Doug Felt71b8dd72010-02-16 17:27:09 -0800817 bounds.bottom = getLineTop(line + 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819 return getLineBaseline(line);
820 }
821
822 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800823 * Return the vertical position of the top of the specified line
824 * (0&hellip;getLineCount()).
825 * If the specified line is equal to the line count, returns the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 * bottom of the last line.
827 */
828 public abstract int getLineTop(int line);
829
830 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800831 * Return the descent of the specified line(0&hellip;getLineCount() - 1).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 */
833 public abstract int getLineDescent(int line);
834
835 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800836 * Return the text offset of the beginning of the specified line (
837 * 0&hellip;getLineCount()). If the specified line is equal to the line
838 * count, returns the length of the text.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 */
840 public abstract int getLineStart(int line);
841
842 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800843 * Returns the primary directionality of the paragraph containing the
844 * specified line, either 1 for left-to-right lines, or -1 for right-to-left
845 * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 */
847 public abstract int getParagraphDirection(int line);
848
849 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700850 * Returns whether the specified line contains one or more
Roozbeh Pournader112d9c72015-08-07 12:44:41 -0700851 * characters that need to be handled specially, like tabs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 */
853 public abstract boolean getLineContainsTab(int line);
854
855 /**
Doug Felt71b8dd72010-02-16 17:27:09 -0800856 * Returns the directional run information for the specified line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 * The array alternates counts of characters in left-to-right
858 * and right-to-left segments of the line.
Doug Felt71b8dd72010-02-16 17:27:09 -0800859 *
860 * <p>NOTE: this is inadequate to support bidirectional text, and will change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 */
862 public abstract Directions getLineDirections(int line);
863
864 /**
865 * Returns the (negative) number of extra pixels of ascent padding in the
866 * top line of the Layout.
867 */
868 public abstract int getTopPadding();
869
870 /**
871 * Returns the number of extra pixels of descent padding in the
872 * bottom line of the Layout.
873 */
874 public abstract int getBottomPadding();
875
Raph Levien26d443a2015-03-30 14:18:32 -0700876 /**
877 * Returns the hyphen edit for a line.
878 *
879 * @hide
880 */
881 public int getHyphen(int line) {
882 return 0;
883 }
884
Raph Levien2ea52902015-07-01 14:39:31 -0700885 /**
886 * Returns the left indent for a line.
887 *
888 * @hide
889 */
890 public int getIndentAdjust(int line, Alignment alignment) {
891 return 0;
892 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700893
894 /**
895 * Returns true if the character at offset and the preceding character
896 * are at different run levels (and thus there's a split caret).
897 * @param offset the offset
898 * @return true if at a level boundary
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800899 * @hide
Doug Felt4e0c5e52010-03-15 16:56:02 -0700900 */
Gilles Debunnef75c97e2011-02-10 16:09:53 -0800901 public boolean isLevelBoundary(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -0800902 int line = getLineForOffset(offset);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700903 Directions dirs = getLineDirections(line);
904 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
905 return false;
906 }
907
908 int[] runs = dirs.mDirections;
Doug Felt9f7a4442010-03-01 12:45:56 -0800909 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700910 int lineEnd = getLineEnd(line);
911 if (offset == lineStart || offset == lineEnd) {
912 int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1;
913 int runIndex = offset == lineStart ? 0 : runs.length - 2;
914 return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel;
915 }
916
917 offset -= lineStart;
Doug Felt9f7a4442010-03-01 12:45:56 -0800918 for (int i = 0; i < runs.length; i += 2) {
Doug Felt4e0c5e52010-03-15 16:56:02 -0700919 if (offset == runs[i]) {
920 return true;
Doug Felt9f7a4442010-03-01 12:45:56 -0800921 }
922 }
Doug Felt4e0c5e52010-03-15 16:56:02 -0700923 return false;
Doug Felt9f7a4442010-03-01 12:45:56 -0800924 }
925
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700926 /**
927 * Returns true if the character at offset is right to left (RTL).
928 * @param offset the offset
929 * @return true if the character is RTL, false if it is LTR
930 */
931 public boolean isRtlCharAt(int offset) {
932 int line = getLineForOffset(offset);
933 Directions dirs = getLineDirections(line);
934 if (dirs == DIRS_ALL_LEFT_TO_RIGHT) {
935 return false;
936 }
937 if (dirs == DIRS_ALL_RIGHT_TO_LEFT) {
938 return true;
939 }
940 int[] runs = dirs.mDirections;
941 int lineStart = getLineStart(line);
942 for (int i = 0; i < runs.length; i += 2) {
Raph Levien87506202014-09-04 12:47:03 -0700943 int start = lineStart + runs[i];
944 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
945 if (offset >= start && offset < limit) {
Fabrice Di Meglio34d2eba2011-08-31 19:46:15 -0700946 int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
947 return ((level & 1) != 0);
948 }
949 }
950 // Should happen only if the offset is "out of bounds"
951 return false;
952 }
953
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +0900954 /**
955 * Returns the range of the run that the character at offset belongs to.
956 * @param offset the offset
957 * @return The range of the run
958 * @hide
959 */
960 public long getRunRange(int offset) {
961 int line = getLineForOffset(offset);
962 Directions dirs = getLineDirections(line);
963 if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) {
964 return TextUtils.packRangeInLong(0, getLineEnd(line));
965 }
966 int[] runs = dirs.mDirections;
967 int lineStart = getLineStart(line);
968 for (int i = 0; i < runs.length; i += 2) {
969 int start = lineStart + runs[i];
970 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
971 if (offset >= start && offset < limit) {
972 return TextUtils.packRangeInLong(start, limit);
973 }
974 }
975 // Should happen only if the offset is "out of bounds"
976 return TextUtils.packRangeInLong(0, getLineEnd(line));
977 }
978
Doug Felt9f7a4442010-03-01 12:45:56 -0800979 private boolean primaryIsTrailingPrevious(int offset) {
980 int line = getLineForOffset(offset);
981 int lineStart = getLineStart(line);
Doug Felt4e0c5e52010-03-15 16:56:02 -0700982 int lineEnd = getLineEnd(line);
Doug Felt9f7a4442010-03-01 12:45:56 -0800983 int[] runs = getLineDirections(line).mDirections;
984
985 int levelAt = -1;
986 for (int i = 0; i < runs.length; i += 2) {
987 int start = lineStart + runs[i];
988 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
989 if (limit > lineEnd) {
990 limit = lineEnd;
991 }
992 if (offset >= start && offset < limit) {
993 if (offset > start) {
994 // Previous character is at same level, so don't use trailing.
995 return false;
996 }
997 levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
998 break;
999 }
1000 }
1001 if (levelAt == -1) {
1002 // Offset was limit of line.
1003 levelAt = getParagraphDirection(line) == 1 ? 0 : 1;
1004 }
1005
1006 // At level boundary, check previous level.
1007 int levelBefore = -1;
1008 if (offset == lineStart) {
1009 levelBefore = getParagraphDirection(line) == 1 ? 0 : 1;
1010 } else {
1011 offset -= 1;
1012 for (int i = 0; i < runs.length; i += 2) {
1013 int start = lineStart + runs[i];
1014 int limit = start + (runs[i+1] & RUN_LENGTH_MASK);
1015 if (limit > lineEnd) {
1016 limit = lineEnd;
1017 }
1018 if (offset >= start && offset < limit) {
1019 levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK;
1020 break;
1021 }
1022 }
1023 }
1024
1025 return levelBefore < levelAt;
1026 }
1027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /**
1029 * Get the primary horizontal position for the specified text offset.
1030 * This is the location where a new character would be inserted in
1031 * the paragraph's primary direction.
1032 */
1033 public float getPrimaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001034 return getPrimaryHorizontal(offset, false /* not clamped */);
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.
1040 * @hide
1041 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001042 public float getPrimaryHorizontal(int offset, boolean clamped) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001043 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001044 return getHorizontal(offset, trailing, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046
1047 /**
1048 * Get the secondary horizontal position for the specified text offset.
1049 * This is the location where a new character would be inserted in
1050 * the direction other than the paragraph's primary direction.
1051 */
1052 public float getSecondaryHorizontal(int offset) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001053 return getSecondaryHorizontal(offset, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Raph Levienafe8e9b2012-12-19 16:09:32 -08001056 /**
1057 * Get the secondary horizontal position for the specified text offset, but
1058 * optionally clamp it so that it doesn't exceed the width of the layout.
1059 * @hide
1060 */
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001061 public float getSecondaryHorizontal(int offset, boolean clamped) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001062 boolean trailing = primaryIsTrailingPrevious(offset);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001063 return getHorizontal(offset, !trailing, clamped);
Raph Levienafe8e9b2012-12-19 16:09:32 -08001064 }
1065
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001066 private float getHorizontal(int offset, boolean primary) {
1067 return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset);
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001068 }
1069
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001070 private float getHorizontal(int offset, boolean trailing, boolean clamped) {
1071 int line = getLineForOffset(offset);
1072
Raph Levienafe8e9b2012-12-19 16:09:32 -08001073 return getHorizontal(offset, trailing, line, clamped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075
Raph Levienafe8e9b2012-12-19 16:09:32 -08001076 private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001078 int end = getLineEnd(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 int dir = getParagraphDirection(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001080 boolean hasTab = getLineContainsTab(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 Directions directions = getLineDirections(line);
1082
Doug Feltc982f602010-05-25 11:51:40 -07001083 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001084 if (hasTab && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001085 // Just checking this line should be good enough, tabs should be
1086 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001087 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001088 if (tabs.length > 0) {
1089 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092
Doug Felte8e45f22010-03-29 14:58:40 -07001093 TextLine tl = TextLine.obtain();
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001094 tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops);
Doug Felte8e45f22010-03-29 14:58:40 -07001095 float wid = tl.measure(offset - start, trailing, null);
1096 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097
Raph Levienafe8e9b2012-12-19 16:09:32 -08001098 if (clamped && wid > mWidth) {
1099 wid = mWidth;
1100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 int left = getParagraphLeft(line);
1102 int right = getParagraphRight(line);
1103
Doug Feltc982f602010-05-25 11:51:40 -07001104 return getLineStartPos(line, left, right) + wid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
1107 /**
1108 * Get the leftmost position that should be exposed for horizontal
1109 * scrolling on the specified line.
1110 */
1111 public float getLineLeft(int line) {
1112 int dir = getParagraphDirection(line);
1113 Alignment align = getParagraphAlignment(line);
1114
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001115 if (align == Alignment.ALIGN_LEFT) {
1116 return 0;
1117 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 if (dir == DIR_RIGHT_TO_LEFT)
1119 return getParagraphRight(line) - getLineMax(line);
1120 else
1121 return 0;
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001122 } else if (align == Alignment.ALIGN_RIGHT) {
1123 return mWidth - getLineMax(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 } else if (align == Alignment.ALIGN_OPPOSITE) {
1125 if (dir == DIR_RIGHT_TO_LEFT)
1126 return 0;
1127 else
1128 return mWidth - getLineMax(line);
1129 } else { /* align == Alignment.ALIGN_CENTER */
1130 int left = getParagraphLeft(line);
1131 int right = getParagraphRight(line);
1132 int max = ((int) getLineMax(line)) & ~1;
1133
1134 return left + ((right - left) - max) / 2;
1135 }
1136 }
1137
1138 /**
1139 * Get the rightmost position that should be exposed for horizontal
1140 * scrolling on the specified line.
1141 */
1142 public float getLineRight(int line) {
1143 int dir = getParagraphDirection(line);
1144 Alignment align = getParagraphAlignment(line);
1145
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001146 if (align == Alignment.ALIGN_LEFT) {
1147 return getParagraphLeft(line) + getLineMax(line);
1148 } else if (align == Alignment.ALIGN_NORMAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 if (dir == DIR_RIGHT_TO_LEFT)
1150 return mWidth;
1151 else
1152 return getParagraphLeft(line) + getLineMax(line);
Doug Feltc0ccf0c2011-06-23 16:13:18 -07001153 } else if (align == Alignment.ALIGN_RIGHT) {
1154 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 } else if (align == Alignment.ALIGN_OPPOSITE) {
1156 if (dir == DIR_RIGHT_TO_LEFT)
1157 return getLineMax(line);
1158 else
1159 return mWidth;
1160 } else { /* align == Alignment.ALIGN_CENTER */
1161 int left = getParagraphLeft(line);
1162 int right = getParagraphRight(line);
1163 int max = ((int) getLineMax(line)) & ~1;
1164
1165 return right - ((right - left) - max) / 2;
1166 }
1167 }
1168
1169 /**
Doug Felt0c702b82010-05-14 10:55:42 -07001170 * Gets the unsigned horizontal extent of the specified line, including
Doug Feltc982f602010-05-25 11:51:40 -07001171 * leading margin indent, but excluding trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 */
1173 public float getLineMax(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001174 float margin = getParagraphLeadingMargin(line);
1175 float signedExtent = getLineExtent(line, false);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001176 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178
1179 /**
Doug Feltc982f602010-05-25 11:51:40 -07001180 * Gets the unsigned horizontal extent of the specified line, including
1181 * leading margin indent and trailing whitespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 */
1183 public float getLineWidth(int line) {
Doug Feltc982f602010-05-25 11:51:40 -07001184 float margin = getParagraphLeadingMargin(line);
1185 float signedExtent = getLineExtent(line, true);
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001186 return margin + (signedExtent >= 0 ? signedExtent : -signedExtent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188
Doug Feltc982f602010-05-25 11:51:40 -07001189 /**
1190 * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the
1191 * tab stops instead of using the ones passed in.
1192 * @param line the index of the line
1193 * @param full whether to include trailing whitespace
1194 * @return the extent of the line
1195 */
1196 private float getLineExtent(int line, boolean full) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 int start = getLineStart(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001198 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Doug Feltc982f602010-05-25 11:51:40 -07001199
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001200 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001201 TabStops tabStops = null;
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001202 if (hasTabs && mText instanceof Spanned) {
Doug Feltc982f602010-05-25 11:51:40 -07001203 // Just checking this line should be good enough, tabs should be
1204 // consistent across all lines in a paragraph.
Eric Fischer74d31ef2010-08-05 15:29:36 -07001205 TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class);
Doug Feltc982f602010-05-25 11:51:40 -07001206 if (tabs.length > 0) {
1207 tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse
1208 }
1209 }
Doug Felte8e45f22010-03-29 14:58:40 -07001210 Directions directions = getLineDirections(line);
Fabrice Di Meglio8059e0902011-08-10 16:31:58 -07001211 // Returned directions can actually be null
1212 if (directions == null) {
1213 return 0f;
1214 }
Doug Feltc982f602010-05-25 11:51:40 -07001215 int dir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216
Doug Felte8e45f22010-03-29 14:58:40 -07001217 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001218 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001219 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001220 if (isJustificationRequired(line)) {
1221 tl.justify(getJustifyWidth(line));
1222 }
Doug Feltc982f602010-05-25 11:51:40 -07001223 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001224 mPaint.setHyphenEdit(0);
Doug Feltc982f602010-05-25 11:51:40 -07001225 TextLine.recycle(tl);
1226 return width;
1227 }
1228
1229 /**
1230 * Returns the signed horizontal extent of the specified line, excluding
1231 * leading margin. If full is false, excludes trailing whitespace.
1232 * @param line the index of the line
1233 * @param tabStops the tab stops, can be null if we know they're not used.
1234 * @param full whether to include trailing whitespace
1235 * @return the extent of the text on this line
1236 */
1237 private float getLineExtent(int line, TabStops tabStops, boolean full) {
1238 int start = getLineStart(line);
1239 int end = full ? getLineEnd(line) : getLineVisibleEnd(line);
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001240 boolean hasTabs = getLineContainsTab(line);
Doug Feltc982f602010-05-25 11:51:40 -07001241 Directions directions = getLineDirections(line);
1242 int dir = getParagraphDirection(line);
1243
1244 TextLine tl = TextLine.obtain();
Seigo Nonaka9863f672016-12-14 17:56:10 +09001245 mPaint.setHyphenEdit(getHyphen(line));
Roozbeh Pournader112d9c72015-08-07 12:44:41 -07001246 tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001247 if (isJustificationRequired(line)) {
1248 tl.justify(getJustifyWidth(line));
1249 }
Doug Felte8e45f22010-03-29 14:58:40 -07001250 float width = tl.metrics(null);
Seigo Nonaka9863f672016-12-14 17:56:10 +09001251 mPaint.setHyphenEdit(0);
Doug Felte8e45f22010-03-29 14:58:40 -07001252 TextLine.recycle(tl);
1253 return width;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255
1256 /**
1257 * Get the line number corresponding to the specified vertical position.
1258 * If you ask for a position above 0, you get 0; if you ask for a position
1259 * below the bottom of the text, you get the last line.
1260 */
1261 // FIXME: It may be faster to do a linear search for layouts without many lines.
1262 public int getLineForVertical(int vertical) {
1263 int high = getLineCount(), low = -1, guess;
1264
1265 while (high - low > 1) {
1266 guess = (high + low) / 2;
1267
1268 if (getLineTop(guess) > vertical)
1269 high = guess;
1270 else
1271 low = guess;
1272 }
1273
1274 if (low < 0)
1275 return 0;
1276 else
1277 return low;
1278 }
1279
1280 /**
1281 * Get the line number on which the specified text offset appears.
1282 * If you ask for a position before 0, you get 0; if you ask for a position
1283 * beyond the end of the text, you get the last line.
1284 */
1285 public int getLineForOffset(int offset) {
1286 int high = getLineCount(), low = -1, guess;
1287
1288 while (high - low > 1) {
1289 guess = (high + low) / 2;
1290
1291 if (getLineStart(guess) > offset)
1292 high = guess;
1293 else
1294 low = guess;
1295 }
1296
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001297 if (low < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 return 0;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001299 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 return low;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
1303
1304 /**
Doug Felt9f7a4442010-03-01 12:45:56 -08001305 * Get the character offset on the specified line whose position is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 * closest to the specified horizontal position.
1307 */
1308 public int getOffsetForHorizontal(int line, float horiz) {
Keisuke Kuroyanagif0bb87b72016-02-08 23:52:55 +09001309 return getOffsetForHorizontal(line, horiz, true);
1310 }
1311
1312 /**
1313 * Get the character offset on the specified line whose position is
1314 * closest to the specified horizontal position.
1315 *
1316 * @param line the line used to find the closest offset
1317 * @param horiz the horizontal position used to find the closest offset
1318 * @param primary whether to use the primary position or secondary position to find the offset
1319 *
1320 * @hide
1321 */
1322 public int getOffsetForHorizontal(int line, float horiz, boolean primary) {
Raph Levienedb27f12015-06-01 14:34:47 -07001323 // TODO: use Paint.getOffsetForAdvance to avoid binary search
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001324 final int lineEndOffset = getLineEnd(line);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001325 final int lineStartOffset = getLineStart(line);
1326
1327 Directions dirs = getLineDirections(line);
1328
1329 TextLine tl = TextLine.obtain();
1330 // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here.
1331 tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs,
1332 false, null);
1333
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001334 final int max;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001335 if (line == getLineCount() - 1) {
1336 max = lineEndOffset;
1337 } else {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001338 max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset,
1339 !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset;
Keisuke Kuroyanagi00ad16d2015-08-27 18:15:48 +09001340 }
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001341 int best = lineStartOffset;
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001342 float bestdist = Math.abs(getHorizontal(best, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
Doug Felt9f7a4442010-03-01 12:45:56 -08001344 for (int i = 0; i < dirs.mDirections.length; i += 2) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001345 int here = lineStartOffset + dirs.mDirections[i];
Doug Felt9f7a4442010-03-01 12:45:56 -08001346 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001347 boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0;
1348 int swap = isRtl ? -1 : 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 if (there > max)
1351 there = max;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 int high = there - 1 + 1, low = here + 1 - 1, guess;
1353
1354 while (high - low > 1) {
1355 guess = (high + low) / 2;
1356 int adguess = getOffsetAtStartOf(guess);
1357
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001358 if (getHorizontal(adguess, primary) * swap >= horiz * swap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 high = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001360 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 low = guess;
Keisuke Kuroyanagi7c263dd2017-01-12 19:24:18 +09001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364
1365 if (low < here + 1)
1366 low = here + 1;
1367
1368 if (low < there) {
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001369 int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset;
1370 low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset;
1371 if (low >= here && low < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001372 float dist = Math.abs(getHorizontal(low, primary) - horiz);
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001373 if (aft < there) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001374 float other = Math.abs(getHorizontal(aft, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001376 if (other < dist) {
1377 dist = other;
1378 low = aft;
1379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001382 if (dist < bestdist) {
1383 bestdist = dist;
1384 best = low;
1385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387 }
1388
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001389 float dist = Math.abs(getHorizontal(here, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390
1391 if (dist < bestdist) {
1392 bestdist = dist;
1393 best = here;
1394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001397 float dist = Math.abs(getHorizontal(max, primary) - horiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398
Raph Levien373b7a82013-09-20 15:11:52 -07001399 if (dist <= bestdist) {
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001400 bestdist = dist;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 best = max;
1402 }
1403
Keisuke Kuroyanagi5bff01d2016-01-08 19:55:17 +09001404 TextLine.recycle(tl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 return best;
1406 }
1407
1408 /**
1409 * Return the text offset after the last character on the specified line.
1410 */
1411 public final int getLineEnd(int line) {
1412 return getLineStart(line + 1);
1413 }
1414
Doug Felt9f7a4442010-03-01 12:45:56 -08001415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 * Return the text offset after the last visible character (so whitespace
1417 * is not counted) on the specified line.
1418 */
1419 public int getLineVisibleEnd(int line) {
1420 return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1));
1421 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 private int getLineVisibleEnd(int line, int start, int end) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 CharSequence text = mText;
1425 char ch;
1426 if (line == getLineCount() - 1) {
1427 return end;
1428 }
1429
1430 for (; end > start; end--) {
1431 ch = text.charAt(end - 1);
1432
1433 if (ch == '\n') {
1434 return end - 1;
1435 }
1436
Seigo Nonaka09da71a2016-11-28 16:24:14 +09001437 if (!TextLine.isLineEndSpace(ch)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 break;
1439 }
1440
1441 }
1442
1443 return end;
1444 }
1445
1446 /**
1447 * Return the vertical position of the bottom of the specified line.
1448 */
1449 public final int getLineBottom(int line) {
1450 return getLineTop(line + 1);
1451 }
1452
1453 /**
1454 * Return the vertical position of the baseline of the specified line.
1455 */
1456 public final int getLineBaseline(int line) {
1457 // getLineTop(line+1) == getLineTop(line)
1458 return getLineTop(line+1) - getLineDescent(line);
1459 }
1460
1461 /**
1462 * Get the ascent of the text on the specified line.
1463 * The return value is negative to match the Paint.ascent() convention.
1464 */
1465 public final int getLineAscent(int line) {
1466 // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line)
1467 return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line));
1468 }
1469
Siyamed Sinir0fa89d62017-07-24 20:46:41 -07001470 /**
1471 * Return the extra space added as a result of line spacing attributes
1472 * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}.
1473 *
1474 * @param line the index of the line, the value should be equal or greater than {@code zero}
1475 * @hide
1476 */
1477 public int getLineExtra(@IntRange(from = 0) int line) {
1478 return 0;
1479 }
1480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 public int getOffsetToLeftOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001482 return getOffsetToLeftRightOf(offset, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 public int getOffsetToRightOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001486 return getOffsetToLeftRightOf(offset, false);
1487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488
Doug Felt9f7a4442010-03-01 12:45:56 -08001489 private int getOffsetToLeftRightOf(int caret, boolean toLeft) {
1490 int line = getLineForOffset(caret);
1491 int lineStart = getLineStart(line);
1492 int lineEnd = getLineEnd(line);
Doug Felte8e45f22010-03-29 14:58:40 -07001493 int lineDir = getParagraphDirection(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001495 boolean lineChanged = false;
Doug Felte8e45f22010-03-29 14:58:40 -07001496 boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT);
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001497 // if walking off line, look at the line we're headed to
1498 if (advance) {
1499 if (caret == lineEnd) {
Doug Felte8e45f22010-03-29 14:58:40 -07001500 if (line < getLineCount() - 1) {
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001501 lineChanged = true;
Doug Felte8e45f22010-03-29 14:58:40 -07001502 ++line;
1503 } else {
1504 return caret; // at very end, don't move
1505 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001506 }
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001507 } else {
1508 if (caret == lineStart) {
1509 if (line > 0) {
1510 lineChanged = true;
1511 --line;
1512 } else {
1513 return caret; // at very start, don't move
1514 }
1515 }
1516 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001517
Gilles Debunne162bf0f2010-11-16 16:23:53 -08001518 if (lineChanged) {
Doug Felte8e45f22010-03-29 14:58:40 -07001519 lineStart = getLineStart(line);
1520 lineEnd = getLineEnd(line);
1521 int newDir = getParagraphDirection(line);
1522 if (newDir != lineDir) {
1523 // unusual case. we want to walk onto the line, but it runs
1524 // in a different direction than this one, so we fake movement
1525 // in the opposite direction.
1526 toLeft = !toLeft;
1527 lineDir = newDir;
1528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
Doug Felt9f7a4442010-03-01 12:45:56 -08001530
Doug Felte8e45f22010-03-29 14:58:40 -07001531 Directions directions = getLineDirections(line);
Doug Felt9f7a4442010-03-01 12:45:56 -08001532
Doug Felte8e45f22010-03-29 14:58:40 -07001533 TextLine tl = TextLine.obtain();
1534 // XXX: we don't care about tabs
1535 tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null);
1536 caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft);
1537 tl = TextLine.recycle(tl);
1538 return caret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540
1541 private int getOffsetAtStartOf(int offset) {
Doug Felt9f7a4442010-03-01 12:45:56 -08001542 // XXX this probably should skip local reorderings and
1543 // zero-width characters, look at callers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (offset == 0)
1545 return 0;
1546
1547 CharSequence text = mText;
1548 char c = text.charAt(offset);
1549
1550 if (c >= '\uDC00' && c <= '\uDFFF') {
1551 char c1 = text.charAt(offset - 1);
1552
1553 if (c1 >= '\uD800' && c1 <= '\uDBFF')
1554 offset -= 1;
1555 }
1556
1557 if (mSpannedText) {
1558 ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1559 ReplacementSpan.class);
1560
1561 for (int i = 0; i < spans.length; i++) {
1562 int start = ((Spanned) text).getSpanStart(spans[i]);
1563 int end = ((Spanned) text).getSpanEnd(spans[i]);
1564
1565 if (start < offset && end > offset)
1566 offset = start;
1567 }
1568 }
1569
1570 return offset;
1571 }
1572
1573 /**
Raph Levienafe8e9b2012-12-19 16:09:32 -08001574 * Determine whether we should clamp cursor position. Currently it's
1575 * only robust for left-aligned displays.
1576 * @hide
1577 */
1578 public boolean shouldClampCursor(int line) {
1579 // Only clamp cursor position in left-aligned displays.
1580 switch (getParagraphAlignment(line)) {
1581 case ALIGN_LEFT:
1582 return true;
1583 case ALIGN_NORMAL:
1584 return getParagraphDirection(line) > 0;
1585 default:
1586 return false;
1587 }
1588
1589 }
1590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 * Fills in the specified Path with a representation of a cursor
1592 * at the specified offset. This will often be a vertical line
Doug Felt4e0c5e52010-03-15 16:56:02 -07001593 * but can be multiple discontinuous lines in text with multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 * directionalities.
1595 */
1596 public void getCursorPath(int point, Path dest,
1597 CharSequence editingBuffer) {
1598 dest.reset();
1599
1600 int line = getLineForOffset(point);
1601 int top = getLineTop(line);
1602 int bottom = getLineTop(line+1);
1603
Raph Levienafe8e9b2012-12-19 16:09:32 -08001604 boolean clamped = shouldClampCursor(line);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001605 float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
1606 float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
Jeff Brown497a92c2010-09-12 17:55:08 -07001608 int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) |
1609 TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING);
1610 int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 int dist = 0;
1612
1613 if (caps != 0 || fn != 0) {
1614 dist = (bottom - top) >> 2;
1615
1616 if (fn != 0)
1617 top += dist;
1618 if (caps != 0)
1619 bottom -= dist;
1620 }
1621
1622 if (h1 < 0.5f)
1623 h1 = 0.5f;
1624 if (h2 < 0.5f)
1625 h2 = 0.5f;
1626
Jozef BABJAK2fb503f2011-03-17 09:54:51 +01001627 if (Float.compare(h1, h2) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 dest.moveTo(h1, top);
1629 dest.lineTo(h1, bottom);
1630 } else {
1631 dest.moveTo(h1, top);
1632 dest.lineTo(h1, (top + bottom) >> 1);
1633
1634 dest.moveTo(h2, (top + bottom) >> 1);
1635 dest.lineTo(h2, bottom);
1636 }
1637
1638 if (caps == 2) {
1639 dest.moveTo(h2, bottom);
1640 dest.lineTo(h2 - dist, bottom + dist);
1641 dest.lineTo(h2, bottom);
1642 dest.lineTo(h2 + dist, bottom + dist);
1643 } else if (caps == 1) {
1644 dest.moveTo(h2, bottom);
1645 dest.lineTo(h2 - dist, bottom + dist);
1646
1647 dest.moveTo(h2 - dist, bottom + dist - 0.5f);
1648 dest.lineTo(h2 + dist, bottom + dist - 0.5f);
1649
1650 dest.moveTo(h2 + dist, bottom + dist);
1651 dest.lineTo(h2, bottom);
1652 }
1653
1654 if (fn == 2) {
1655 dest.moveTo(h1, top);
1656 dest.lineTo(h1 - dist, top - dist);
1657 dest.lineTo(h1, top);
1658 dest.lineTo(h1 + dist, top - dist);
1659 } else if (fn == 1) {
1660 dest.moveTo(h1, top);
1661 dest.lineTo(h1 - dist, top - dist);
1662
1663 dest.moveTo(h1 - dist, top - dist + 0.5f);
1664 dest.lineTo(h1 + dist, top - dist + 0.5f);
1665
1666 dest.moveTo(h1 + dist, top - dist);
1667 dest.lineTo(h1, top);
1668 }
1669 }
1670
1671 private void addSelection(int line, int start, int end,
1672 int top, int bottom, Path dest) {
1673 int linestart = getLineStart(line);
1674 int lineend = getLineEnd(line);
1675 Directions dirs = getLineDirections(line);
1676
1677 if (lineend > linestart && mText.charAt(lineend - 1) == '\n')
1678 lineend--;
1679
Doug Felt9f7a4442010-03-01 12:45:56 -08001680 for (int i = 0; i < dirs.mDirections.length; i += 2) {
1681 int here = linestart + dirs.mDirections[i];
1682 int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK);
1683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (there > lineend)
1685 there = lineend;
1686
1687 if (start <= there && end >= here) {
1688 int st = Math.max(start, here);
1689 int en = Math.min(end, there);
1690
1691 if (st != en) {
Raph Levienafe8e9b2012-12-19 16:09:32 -08001692 float h1 = getHorizontal(st, false, line, false /* not clamped */);
1693 float h2 = getHorizontal(en, true, line, false /* not clamped */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
Fabrice Di Meglio37166012011-08-31 13:56:37 -07001695 float left = Math.min(h1, h2);
1696 float right = Math.max(h1, h2);
1697
1698 dest.addRect(left, top, right, bottom, Path.Direction.CW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702 }
1703
1704 /**
1705 * Fills in the specified Path with a representation of a highlight
1706 * between the specified offsets. This will often be a rectangle
1707 * or a potentially discontinuous set of rectangles. If the start
1708 * and end are the same, the returned path is empty.
1709 */
1710 public void getSelectionPath(int start, int end, Path dest) {
1711 dest.reset();
1712
1713 if (start == end)
1714 return;
1715
1716 if (end < start) {
1717 int temp = end;
1718 end = start;
1719 start = temp;
1720 }
1721
1722 int startline = getLineForOffset(start);
Roozbeh Pournader7557a5a2017-04-11 18:34:42 -07001723 int endline = getLineForOffset(end);
1724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 int top = getLineTop(startline);
1726 int bottom = getLineBottom(endline);
1727
1728 if (startline == endline) {
1729 addSelection(startline, start, end, top, bottom, dest);
1730 } else {
1731 final float width = mWidth;
1732
1733 addSelection(startline, start, getLineEnd(startline),
1734 top, getLineBottom(startline), dest);
Doug Felt9f7a4442010-03-01 12:45:56 -08001735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT)
1737 dest.addRect(getLineLeft(startline), top,
1738 0, getLineBottom(startline), Path.Direction.CW);
1739 else
1740 dest.addRect(getLineRight(startline), top,
1741 width, getLineBottom(startline), Path.Direction.CW);
1742
1743 for (int i = startline + 1; i < endline; i++) {
1744 top = getLineTop(i);
1745 bottom = getLineBottom(i);
1746 dest.addRect(0, top, width, bottom, Path.Direction.CW);
1747 }
1748
1749 top = getLineTop(endline);
1750 bottom = getLineBottom(endline);
1751
1752 addSelection(endline, getLineStart(endline), end,
1753 top, bottom, dest);
1754
1755 if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT)
1756 dest.addRect(width, top, getLineRight(endline), bottom, Path.Direction.CW);
1757 else
1758 dest.addRect(0, top, getLineLeft(endline), bottom, Path.Direction.CW);
1759 }
1760 }
1761
1762 /**
1763 * Get the alignment of the specified paragraph, taking into account
1764 * markup attached to it.
1765 */
1766 public final Alignment getParagraphAlignment(int line) {
1767 Alignment align = mAlignment;
1768
1769 if (mSpannedText) {
1770 Spanned sp = (Spanned) mText;
Eric Fischer74d31ef2010-08-05 15:29:36 -07001771 AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 getLineEnd(line),
1773 AlignmentSpan.class);
1774
1775 int spanLength = spans.length;
1776 if (spanLength > 0) {
1777 align = spans[spanLength-1].getAlignment();
1778 }
1779 }
1780
1781 return align;
1782 }
1783
1784 /**
1785 * Get the left edge of the specified paragraph, inset by left margins.
1786 */
1787 public final int getParagraphLeft(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 int left = 0;
Doug Feltc982f602010-05-25 11:51:40 -07001789 int dir = getParagraphDirection(line);
1790 if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) {
1791 return left; // leading margin has no impact, or no styles
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
Doug Feltc982f602010-05-25 11:51:40 -07001793 return getParagraphLeadingMargin(line);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795
1796 /**
1797 * Get the right edge of the specified paragraph, inset by right margins.
1798 */
1799 public final int getParagraphRight(int line) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 int right = mWidth;
Doug Feltc982f602010-05-25 11:51:40 -07001801 int dir = getParagraphDirection(line);
1802 if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) {
1803 return right; // leading margin has no impact, or no styles
1804 }
1805 return right - getParagraphLeadingMargin(line);
1806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807
Doug Feltc982f602010-05-25 11:51:40 -07001808 /**
1809 * Returns the effective leading margin (unsigned) for this line,
1810 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
1811 * @param line the line index
1812 * @return the leading margin of this line
1813 */
1814 private int getParagraphLeadingMargin(int line) {
1815 if (!mSpannedText) {
1816 return 0;
1817 }
1818 Spanned spanned = (Spanned) mText;
Doug Felt0c702b82010-05-14 10:55:42 -07001819
Doug Feltc982f602010-05-25 11:51:40 -07001820 int lineStart = getLineStart(line);
1821 int lineEnd = getLineEnd(line);
Doug Felt0c702b82010-05-14 10:55:42 -07001822 int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001823 LeadingMarginSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001824 LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001825 LeadingMarginSpan.class);
1826 if (spans.length == 0) {
1827 return 0; // no leading margin span;
1828 }
Doug Felt0c702b82010-05-14 10:55:42 -07001829
Doug Feltc982f602010-05-25 11:51:40 -07001830 int margin = 0;
Doug Felt0c702b82010-05-14 10:55:42 -07001831
1832 boolean isFirstParaLine = lineStart == 0 ||
Doug Feltc982f602010-05-25 11:51:40 -07001833 spanned.charAt(lineStart - 1) == '\n';
Doug Felt0c702b82010-05-14 10:55:42 -07001834
Anish Athalyeab08c6d2014-08-08 12:09:58 -07001835 boolean useFirstLineMargin = isFirstParaLine;
1836 for (int i = 0; i < spans.length; i++) {
1837 if (spans[i] instanceof LeadingMarginSpan2) {
1838 int spStart = spanned.getSpanStart(spans[i]);
1839 int spanLine = getLineForOffset(spStart);
1840 int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
1841 // if there is more than one LeadingMarginSpan2, use the count that is greatest
1842 useFirstLineMargin |= line < spanLine + count;
1843 }
1844 }
Doug Feltc982f602010-05-25 11:51:40 -07001845 for (int i = 0; i < spans.length; i++) {
1846 LeadingMarginSpan span = spans[i];
Doug Feltc982f602010-05-25 11:51:40 -07001847 margin += span.getLeadingMargin(useFirstLineMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849
Doug Feltc982f602010-05-25 11:51:40 -07001850 return margin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 }
1852
Doug Felte8e45f22010-03-29 14:58:40 -07001853 /* package */
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001854 static float measurePara(TextPaint paint, CharSequence text, int start, int end,
1855 TextDirectionHeuristic textDir) {
Doug Felte8e45f22010-03-29 14:58:40 -07001856 MeasuredText mt = MeasuredText.obtain();
1857 TextLine tl = TextLine.obtain();
1858 try {
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001859 mt.setPara(text, start, end, textDir, null);
Doug Felte8e45f22010-03-29 14:58:40 -07001860 Directions directions;
Doug Feltc982f602010-05-25 11:51:40 -07001861 int dir;
1862 if (mt.mEasy) {
Doug Felte8e45f22010-03-29 14:58:40 -07001863 directions = DIRS_ALL_LEFT_TO_RIGHT;
Doug Feltc982f602010-05-25 11:51:40 -07001864 dir = Layout.DIR_LEFT_TO_RIGHT;
Doug Felte8e45f22010-03-29 14:58:40 -07001865 } else {
1866 directions = AndroidBidi.directions(mt.mDir, mt.mLevels,
1867 0, mt.mChars, 0, mt.mLen);
Doug Feltc982f602010-05-25 11:51:40 -07001868 dir = mt.mDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
Doug Feltc982f602010-05-25 11:51:40 -07001870 char[] chars = mt.mChars;
1871 int len = mt.mLen;
1872 boolean hasTabs = false;
1873 TabStops tabStops = null;
Anish Athalyec14b3ad2014-07-24 18:03:21 -07001874 // leading margins should be taken into account when measuring a paragraph
1875 int margin = 0;
1876 if (text instanceof Spanned) {
1877 Spanned spanned = (Spanned) text;
1878 LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end,
1879 LeadingMarginSpan.class);
1880 for (LeadingMarginSpan lms : spans) {
1881 margin += lms.getLeadingMargin(true);
1882 }
1883 }
Doug Feltc982f602010-05-25 11:51:40 -07001884 for (int i = 0; i < len; ++i) {
1885 if (chars[i] == '\t') {
1886 hasTabs = true;
1887 if (text instanceof Spanned) {
1888 Spanned spanned = (Spanned) text;
Doug Felt0c702b82010-05-14 10:55:42 -07001889 int spanEnd = spanned.nextSpanTransition(start, end,
Doug Feltc982f602010-05-25 11:51:40 -07001890 TabStopSpan.class);
Eric Fischer74d31ef2010-08-05 15:29:36 -07001891 TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd,
Doug Feltc982f602010-05-25 11:51:40 -07001892 TabStopSpan.class);
1893 if (spans.length > 0) {
1894 tabStops = new TabStops(TAB_INCREMENT, spans);
1895 }
1896 }
1897 break;
1898 }
1899 }
1900 tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
Siyamed Sinir79bf9d12016-05-18 19:57:52 -07001901 return margin + Math.abs(tl.metrics(null));
Doug Felte8e45f22010-03-29 14:58:40 -07001902 } finally {
1903 TextLine.recycle(tl);
1904 MeasuredText.recycle(mt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
1907
Doug Felt71b8dd72010-02-16 17:27:09 -08001908 /**
Doug Feltc982f602010-05-25 11:51:40 -07001909 * @hide
1910 */
1911 /* package */ static class TabStops {
1912 private int[] mStops;
1913 private int mNumStops;
1914 private int mIncrement;
Doug Felt0c702b82010-05-14 10:55:42 -07001915
Doug Feltc982f602010-05-25 11:51:40 -07001916 TabStops(int increment, Object[] spans) {
1917 reset(increment, spans);
1918 }
Doug Felt0c702b82010-05-14 10:55:42 -07001919
Doug Feltc982f602010-05-25 11:51:40 -07001920 void reset(int increment, Object[] spans) {
1921 this.mIncrement = increment;
1922
1923 int ns = 0;
1924 if (spans != null) {
1925 int[] stops = this.mStops;
1926 for (Object o : spans) {
1927 if (o instanceof TabStopSpan) {
1928 if (stops == null) {
1929 stops = new int[10];
1930 } else if (ns == stops.length) {
1931 int[] nstops = new int[ns * 2];
1932 for (int i = 0; i < ns; ++i) {
1933 nstops[i] = stops[i];
1934 }
1935 stops = nstops;
1936 }
1937 stops[ns++] = ((TabStopSpan) o).getTabStop();
1938 }
1939 }
1940 if (ns > 1) {
1941 Arrays.sort(stops, 0, ns);
1942 }
1943 if (stops != this.mStops) {
1944 this.mStops = stops;
1945 }
1946 }
1947 this.mNumStops = ns;
1948 }
Doug Felt0c702b82010-05-14 10:55:42 -07001949
Doug Feltc982f602010-05-25 11:51:40 -07001950 float nextTab(float h) {
1951 int ns = this.mNumStops;
1952 if (ns > 0) {
1953 int[] stops = this.mStops;
1954 for (int i = 0; i < ns; ++i) {
1955 int stop = stops[i];
1956 if (stop > h) {
1957 return stop;
1958 }
1959 }
1960 }
1961 return nextDefaultStop(h, mIncrement);
1962 }
1963
1964 public static float nextDefaultStop(float h, int inc) {
1965 return ((int) ((h + inc) / inc)) * inc;
1966 }
1967 }
Doug Felt0c702b82010-05-14 10:55:42 -07001968
Doug Feltc982f602010-05-25 11:51:40 -07001969 /**
Doug Felt71b8dd72010-02-16 17:27:09 -08001970 * Returns the position of the next tab stop after h on the line.
1971 *
1972 * @param text the text
1973 * @param start start of the line
1974 * @param end limit of the line
1975 * @param h the current horizontal offset
1976 * @param tabs the tabs, can be null. If it is null, any tabs in effect
1977 * on the line will be used. If there are no tabs, a default offset
1978 * will be used to compute the tab stop.
1979 * @return the offset of the next tab stop.
1980 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 /* package */ static float nextTab(CharSequence text, int start, int end,
1982 float h, Object[] tabs) {
1983 float nh = Float.MAX_VALUE;
1984 boolean alltabs = false;
1985
1986 if (text instanceof Spanned) {
1987 if (tabs == null) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07001988 tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 alltabs = true;
1990 }
1991
1992 for (int i = 0; i < tabs.length; i++) {
1993 if (!alltabs) {
1994 if (!(tabs[i] instanceof TabStopSpan))
1995 continue;
1996 }
1997
1998 int where = ((TabStopSpan) tabs[i]).getTabStop();
1999
2000 if (where < nh && where > h)
2001 nh = where;
2002 }
2003
2004 if (nh != Float.MAX_VALUE)
2005 return nh;
2006 }
2007
2008 return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT;
2009 }
2010
2011 protected final boolean isSpanned() {
2012 return mSpannedText;
2013 }
2014
Eric Fischer74d31ef2010-08-05 15:29:36 -07002015 /**
2016 * Returns the same as <code>text.getSpans()</code>, except where
2017 * <code>start</code> and <code>end</code> are the same and are not
2018 * at the very beginning of the text, in which case an empty array
2019 * is returned instead.
2020 * <p>
2021 * This is needed because of the special case that <code>getSpans()</code>
2022 * on an empty range returns the spans adjacent to that range, which is
2023 * primarily for the sake of <code>TextWatchers</code> so they will get
2024 * notifications when text goes from empty to non-empty. But it also
2025 * has the unfortunate side effect that if the text ends with an empty
2026 * paragraph, that paragraph accidentally picks up the styles of the
2027 * preceding paragraph (even though those styles will not be picked up
2028 * by new text that is inserted into the empty paragraph).
2029 * <p>
2030 * The reason it just checks whether <code>start</code> and <code>end</code>
2031 * is the same is that the only time a line can contain 0 characters
2032 * is if it is the final paragraph of the Layout; otherwise any line will
2033 * contain at least one printing or newline character. The reason for the
2034 * additional check if <code>start</code> is greater than 0 is that
2035 * if the empty paragraph is the entire content of the buffer, paragraph
2036 * styles that are already applied to the buffer will apply to text that
2037 * is inserted into it.
2038 */
Gilles Debunneeca5b732012-04-25 18:48:42 -07002039 /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) {
Eric Fischer74d31ef2010-08-05 15:29:36 -07002040 if (start == end && start > 0) {
Gilles Debunne6c488de2012-03-01 16:20:35 -08002041 return ArrayUtils.emptyArray(type);
Eric Fischer74d31ef2010-08-05 15:29:36 -07002042 }
2043
Siyamed Sinirfa05ba02016-01-12 10:54:43 -08002044 if(text instanceof SpannableStringBuilder) {
2045 return ((SpannableStringBuilder) text).getSpans(start, end, type, false);
2046 } else {
2047 return text.getSpans(start, end, type);
2048 }
Eric Fischer74d31ef2010-08-05 15:29:36 -07002049 }
2050
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002051 private char getEllipsisChar(TextUtils.TruncateAt method) {
2052 return (method == TextUtils.TruncateAt.END_SMALL) ?
Neil Fullerd29bdb22015-02-06 10:03:08 +00002053 TextUtils.ELLIPSIS_TWO_DOTS[0] :
2054 TextUtils.ELLIPSIS_NORMAL[0];
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002055 }
2056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 private void ellipsize(int start, int end, int line,
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002058 char[] dest, int destoff, TextUtils.TruncateAt method) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int ellipsisCount = getEllipsisCount(line);
2060
2061 if (ellipsisCount == 0) {
2062 return;
2063 }
2064
2065 int ellipsisStart = getEllipsisStart(line);
2066 int linestart = getLineStart(line);
2067
2068 for (int i = ellipsisStart; i < ellipsisStart + ellipsisCount; i++) {
2069 char c;
2070
2071 if (i == ellipsisStart) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002072 c = getEllipsisChar(method); // ellipsis
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 } else {
2074 c = '\uFEFF'; // 0-width space
2075 }
2076
2077 int a = i + linestart;
2078
2079 if (a >= start && a < end) {
2080 dest[destoff + a - start] = c;
2081 }
2082 }
2083 }
2084
2085 /**
2086 * Stores information about bidirectional (left-to-right or right-to-left)
Doug Felt9f7a4442010-03-01 12:45:56 -08002087 * text within the layout of a line.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 */
2089 public static class Directions {
Doug Felt9f7a4442010-03-01 12:45:56 -08002090 // Directions represents directional runs within a line of text.
2091 // Runs are pairs of ints listed in visual order, starting from the
2092 // leading margin. The first int of each pair is the offset from
2093 // the first character of the line to the start of the run. The
2094 // second int represents both the length and level of the run.
2095 // The length is in the lower bits, accessed by masking with
2096 // DIR_LENGTH_MASK. The level is in the higher bits, accessed
2097 // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK.
2098 // To simply test for an RTL direction, test the bit using
2099 // DIR_RTL_FLAG, if set then the direction is rtl.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100
Siyamed Sinired09ae12016-02-16 14:36:26 -08002101 /**
2102 * @hide
2103 */
2104 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2105 public int[] mDirections;
2106
2107 /**
2108 * @hide
2109 */
2110 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2111 public Directions(int[] dirs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 mDirections = dirs;
2113 }
2114 }
2115
2116 /**
2117 * Return the offset of the first character to be ellipsized away,
2118 * relative to the start of the line. (So 0 if the beginning of the
2119 * line is ellipsized, not getLineStart().)
2120 */
2121 public abstract int getEllipsisStart(int line);
Doug Felte8e45f22010-03-29 14:58:40 -07002122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 /**
2124 * Returns the number of characters to be ellipsized away, or 0 if
2125 * no ellipsis is to take place.
2126 */
2127 public abstract int getEllipsisCount(int line);
2128
2129 /* package */ static class Ellipsizer implements CharSequence, GetChars {
2130 /* package */ CharSequence mText;
2131 /* package */ Layout mLayout;
2132 /* package */ int mWidth;
2133 /* package */ TextUtils.TruncateAt mMethod;
2134
2135 public Ellipsizer(CharSequence s) {
2136 mText = s;
2137 }
2138
2139 public char charAt(int off) {
2140 char[] buf = TextUtils.obtain(1);
2141 getChars(off, off + 1, buf, 0);
2142 char ret = buf[0];
2143
2144 TextUtils.recycle(buf);
2145 return ret;
2146 }
2147
2148 public void getChars(int start, int end, char[] dest, int destoff) {
2149 int line1 = mLayout.getLineForOffset(start);
2150 int line2 = mLayout.getLineForOffset(end);
2151
2152 TextUtils.getChars(mText, start, end, dest, destoff);
2153
2154 for (int i = line1; i <= line2; i++) {
Fabrice Di Meglio8d44fff2012-06-13 15:45:38 -07002155 mLayout.ellipsize(start, end, i, dest, destoff, mMethod);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
2157 }
2158
2159 public int length() {
2160 return mText.length();
2161 }
Doug Felt9f7a4442010-03-01 12:45:56 -08002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 public CharSequence subSequence(int start, int end) {
2164 char[] s = new char[end - start];
2165 getChars(start, end, s, 0);
2166 return new String(s);
2167 }
2168
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002169 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 public String toString() {
2171 char[] s = new char[length()];
2172 getChars(0, length(), s, 0);
2173 return new String(s);
2174 }
2175
2176 }
2177
Gilles Debunne6c488de2012-03-01 16:20:35 -08002178 /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 private Spanned mSpanned;
2180
2181 public SpannedEllipsizer(CharSequence display) {
2182 super(display);
2183 mSpanned = (Spanned) display;
2184 }
2185
2186 public <T> T[] getSpans(int start, int end, Class<T> type) {
2187 return mSpanned.getSpans(start, end, type);
2188 }
2189
2190 public int getSpanStart(Object tag) {
2191 return mSpanned.getSpanStart(tag);
2192 }
2193
2194 public int getSpanEnd(Object tag) {
2195 return mSpanned.getSpanEnd(tag);
2196 }
2197
2198 public int getSpanFlags(Object tag) {
2199 return mSpanned.getSpanFlags(tag);
2200 }
2201
Gilles Debunne6c488de2012-03-01 16:20:35 -08002202 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 public int nextSpanTransition(int start, int limit, Class type) {
2204 return mSpanned.nextSpanTransition(start, limit, type);
2205 }
2206
Gilles Debunne162bf0f2010-11-16 16:23:53 -08002207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 public CharSequence subSequence(int start, int end) {
2209 char[] s = new char[end - start];
2210 getChars(start, end, s, 0);
2211
2212 SpannableString ss = new SpannableString(new String(s));
2213 TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0);
2214 return ss;
2215 }
2216 }
2217
2218 private CharSequence mText;
2219 private TextPaint mPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 private int mWidth;
2221 private Alignment mAlignment = Alignment.ALIGN_NORMAL;
2222 private float mSpacingMult;
2223 private float mSpacingAdd;
Romain Guyc4d8eb62010-08-18 20:48:33 -07002224 private static final Rect sTempRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 private boolean mSpannedText;
Doug Feltcb3791202011-07-07 11:57:48 -07002226 private TextDirectionHeuristic mTextDir;
Gilles Debunneeca5b732012-04-25 18:48:42 -07002227 private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
Seigo Nonaka4b4730d2017-03-31 09:42:16 -07002228 private int mJustificationMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229
2230 public static final int DIR_LEFT_TO_RIGHT = 1;
2231 public static final int DIR_RIGHT_TO_LEFT = -1;
Doug Felt9f7a4442010-03-01 12:45:56 -08002232
Doug Felt20178d62010-02-22 13:39:01 -08002233 /* package */ static final int DIR_REQUEST_LTR = 1;
2234 /* package */ static final int DIR_REQUEST_RTL = -1;
2235 /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2;
2236 /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237
Doug Felt9f7a4442010-03-01 12:45:56 -08002238 /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff;
2239 /* package */ static final int RUN_LEVEL_SHIFT = 26;
2240 /* package */ static final int RUN_LEVEL_MASK = 0x3f;
2241 /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT;
2242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 public enum Alignment {
2244 ALIGN_NORMAL,
2245 ALIGN_OPPOSITE,
2246 ALIGN_CENTER,
Doug Feltc0ccf0c2011-06-23 16:13:18 -07002247 /** @hide */
2248 ALIGN_LEFT,
2249 /** @hide */
2250 ALIGN_RIGHT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 }
2252
2253 private static final int TAB_INCREMENT = 20;
2254
Siyamed Sinired09ae12016-02-16 14:36:26 -08002255 /** @hide */
2256 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2257 public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002258 new Directions(new int[] { 0, RUN_LENGTH_MASK });
Siyamed Sinired09ae12016-02-16 14:36:26 -08002259
2260 /** @hide */
2261 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
2262 public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
Doug Felt9f7a4442010-03-01 12:45:56 -08002263 new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
Gilles Debunne0a4db3c2011-01-14 12:12:04 -08002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265}