Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | * 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 | |
| 17 | package android.text; |
| 18 | |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 20 | import android.annotation.IntRange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Paint; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 23 | import android.graphics.Path; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.text.method.TextKeyListener; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 26 | import android.text.style.AlignmentSpan; |
| 27 | import android.text.style.LeadingMarginSpan; |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 28 | import android.text.style.LeadingMarginSpan.LeadingMarginSpan2; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 29 | import android.text.style.LineBackgroundSpan; |
| 30 | import android.text.style.ParagraphStyle; |
| 31 | import android.text.style.ReplacementSpan; |
| 32 | import android.text.style.TabStopSpan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Siyamed Sinir | ed09ae1 | 2016-02-16 14:36:26 -0800 | [diff] [blame] | 34 | import com.android.internal.annotations.VisibleForTesting; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 35 | import com.android.internal.util.ArrayUtils; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 36 | import com.android.internal.util.GrowingArrayUtils; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 37 | |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 38 | import java.lang.annotation.Retention; |
| 39 | import java.lang.annotation.RetentionPolicy; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 40 | import java.util.Arrays; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | /** |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 43 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * For text that will not change, use a {@link StaticLayout}. |
| 48 | */ |
| 49 | public abstract class Layout { |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 50 | /** @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 Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 75 | /** @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 Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 104 | private static final ParagraphStyle[] NO_PARA_SPANS = |
| 105 | ArrayUtils.emptyArray(ParagraphStyle.class); |
Dave Bort | 76c0226 | 2009-04-13 17:17:21 -0700 | [diff] [blame] | 106 | |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 107 | /** @hide */ |
| 108 | @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD}) |
| 109 | @Retention(RetentionPolicy.SOURCE) |
| 110 | public @interface JustificationMode {} |
| 111 | |
| 112 | /** |
| 113 | * Value for justification mode indicating no justification. |
| 114 | */ |
| 115 | public static final int JUSTIFICATION_MODE_NONE = 0; |
| 116 | |
| 117 | /** |
| 118 | * Value for justification mode indicating the text is justified by stretching word spacing. |
| 119 | */ |
| 120 | public static final int JUSTIFICATION_MODE_INTER_WORD = 1; |
| 121 | |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Line spacing multiplier for default line spacing. |
| 124 | */ |
| 125 | public static final float DEFAULT_LINESPACING_MULTIPLIER = 1.0f; |
| 126 | |
| 127 | /* |
| 128 | * Line spacing addition for default line spacing. |
| 129 | */ |
| 130 | public static final float DEFAULT_LINESPACING_ADDITION = 0.0f; |
| 131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | /** |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 133 | * Return how wide a layout must be in order to display the specified text with one line per |
| 134 | * paragraph. |
| 135 | * |
| 136 | * <p>As of O, Uses |
| 137 | * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In |
| 138 | * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | */ |
| 140 | public static float getDesiredWidth(CharSequence source, |
| 141 | TextPaint paint) { |
| 142 | return getDesiredWidth(source, 0, source.length(), paint); |
| 143 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | /** |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 146 | * Return how wide a layout must be in order to display the specified text slice with one |
| 147 | * line per paragraph. |
| 148 | * |
| 149 | * <p>As of O, Uses |
| 150 | * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In |
| 151 | * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p> |
| 152 | */ |
| 153 | public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) { |
| 154 | return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR); |
| 155 | } |
| 156 | |
| 157 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 158 | * Return how wide a layout must be in order to display the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | * specified text slice with one line per paragraph. |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 160 | * |
| 161 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | */ |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 163 | public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint, |
| 164 | TextDirectionHeuristic textDir) { |
Seigo Nonaka | 917748e | 2017-08-03 17:42:28 -0700 | [diff] [blame] | 165 | return getDesiredWidthWithLimit(source, start, end, paint, textDir, Float.MAX_VALUE); |
| 166 | } |
| 167 | /** |
| 168 | * Return how wide a layout must be in order to display the |
| 169 | * specified text slice with one line per paragraph. |
| 170 | * |
| 171 | * If the measured width exceeds given limit, returns limit value instead. |
| 172 | * @hide |
| 173 | */ |
| 174 | public static float getDesiredWidthWithLimit(CharSequence source, int start, int end, |
| 175 | TextPaint paint, TextDirectionHeuristic textDir, float upperLimit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | float need = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | int next; |
| 179 | for (int i = start; i <= end; i = next) { |
| 180 | next = TextUtils.indexOf(source, '\n', i, end); |
| 181 | |
| 182 | if (next < 0) |
| 183 | next = end; |
| 184 | |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 185 | // note, omits trailing paragraph char |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 186 | float w = measurePara(paint, source, i, next, textDir); |
Seigo Nonaka | 917748e | 2017-08-03 17:42:28 -0700 | [diff] [blame] | 187 | if (w > upperLimit) { |
| 188 | return upperLimit; |
| 189 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | if (w > need) |
| 192 | need = w; |
| 193 | |
| 194 | next++; |
| 195 | } |
| 196 | |
| 197 | return need; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Subclasses of Layout use this constructor to set the display text, |
| 202 | * width, and other standard properties. |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 203 | * @param text the text to render |
| 204 | * @param paint the default paint for the layout. Styles can override |
| 205 | * various attributes of the paint. |
| 206 | * @param width the wrapping width for the text. |
| 207 | * @param align whether to left, right, or center the text. Styles can |
| 208 | * override the alignment. |
| 209 | * @param spacingMult factor by which to scale the font size to get the |
| 210 | * default line spacing |
| 211 | * @param spacingAdd amount to add to the default line spacing |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | */ |
| 213 | protected Layout(CharSequence text, TextPaint paint, |
| 214 | int width, Alignment align, |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 215 | float spacingMult, float spacingAdd) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 216 | this(text, paint, width, align, TextDirectionHeuristics.FIRSTSTRONG_LTR, |
| 217 | spacingMult, spacingAdd); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Subclasses of Layout use this constructor to set the display text, |
| 222 | * width, and other standard properties. |
| 223 | * @param text the text to render |
| 224 | * @param paint the default paint for the layout. Styles can override |
| 225 | * various attributes of the paint. |
| 226 | * @param width the wrapping width for the text. |
| 227 | * @param align whether to left, right, or center the text. Styles can |
| 228 | * override the alignment. |
| 229 | * @param spacingMult factor by which to scale the font size to get the |
| 230 | * default line spacing |
| 231 | * @param spacingAdd amount to add to the default line spacing |
| 232 | * |
| 233 | * @hide |
| 234 | */ |
| 235 | protected Layout(CharSequence text, TextPaint paint, |
| 236 | int width, Alignment align, TextDirectionHeuristic textDir, |
| 237 | float spacingMult, float spacingAdd) { |
| 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | if (width < 0) |
| 240 | throw new IllegalArgumentException("Layout: " + width + " < 0"); |
| 241 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 242 | // Ensure paint doesn't have baselineShift set. |
| 243 | // While normally we don't modify the paint the user passed in, |
| 244 | // we were already doing this in Styled.drawUniformRun with both |
| 245 | // baselineShift and bgColor. We probably should reevaluate bgColor. |
| 246 | if (paint != null) { |
| 247 | paint.bgColor = 0; |
| 248 | paint.baselineShift = 0; |
| 249 | } |
| 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | mText = text; |
| 252 | mPaint = paint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | mWidth = width; |
| 254 | mAlignment = align; |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 255 | mSpacingMult = spacingMult; |
| 256 | mSpacingAdd = spacingAdd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | mSpannedText = text instanceof Spanned; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 258 | mTextDir = textDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 261 | /** @hide */ |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 262 | protected void setJustificationMode(@JustificationMode int justificationMode) { |
| 263 | mJustificationMode = justificationMode; |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 264 | } |
| 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Replace constructor properties of this Layout with new ones. Be careful. |
| 268 | */ |
| 269 | /* package */ void replaceWith(CharSequence text, TextPaint paint, |
| 270 | int width, Alignment align, |
| 271 | float spacingmult, float spacingadd) { |
| 272 | if (width < 0) { |
| 273 | throw new IllegalArgumentException("Layout: " + width + " < 0"); |
| 274 | } |
| 275 | |
| 276 | mText = text; |
| 277 | mPaint = paint; |
| 278 | mWidth = width; |
| 279 | mAlignment = align; |
| 280 | mSpacingMult = spacingmult; |
| 281 | mSpacingAdd = spacingadd; |
| 282 | mSpannedText = text instanceof Spanned; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Draw this Layout on the specified Canvas. |
| 287 | */ |
| 288 | public void draw(Canvas c) { |
| 289 | draw(c, null, null, 0); |
| 290 | } |
| 291 | |
| 292 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 293 | * Draw this Layout on the specified canvas, with the highlight path drawn |
| 294 | * between the background and the text. |
| 295 | * |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 296 | * @param canvas the canvas |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 297 | * @param highlight the path of the highlight or cursor; can be null |
| 298 | * @param highlightPaint the paint for the highlight |
| 299 | * @param cursorOffsetVertical the amount to temporarily translate the |
| 300 | * canvas while rendering the highlight |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | */ |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 302 | public void draw(Canvas canvas, Path highlight, Paint highlightPaint, |
| 303 | int cursorOffsetVertical) { |
| 304 | final long lineRange = getLineRangeForDraw(canvas); |
| 305 | int firstLine = TextUtils.unpackRangeStartFromLong(lineRange); |
| 306 | int lastLine = TextUtils.unpackRangeEndFromLong(lineRange); |
| 307 | if (lastLine < 0) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 309 | drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical, |
| 310 | firstLine, lastLine); |
| 311 | drawText(canvas, firstLine, lastLine); |
| 312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 314 | private boolean isJustificationRequired(int lineNum) { |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 315 | if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false; |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 316 | final int lineEnd = getLineEnd(lineNum); |
| 317 | return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n'; |
| 318 | } |
| 319 | |
| 320 | private float getJustifyWidth(int lineNum) { |
| 321 | Alignment paraAlign = mAlignment; |
| 322 | TabStops tabStops = null; |
| 323 | boolean tabStopsIsInitialized = false; |
| 324 | |
| 325 | int left = 0; |
| 326 | int right = mWidth; |
| 327 | |
| 328 | final int dir = getParagraphDirection(lineNum); |
| 329 | |
| 330 | ParagraphStyle[] spans = NO_PARA_SPANS; |
| 331 | if (mSpannedText) { |
| 332 | Spanned sp = (Spanned) mText; |
| 333 | final int start = getLineStart(lineNum); |
| 334 | |
| 335 | final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n'); |
| 336 | |
| 337 | if (isFirstParaLine) { |
| 338 | final int spanEnd = sp.nextSpanTransition(start, mText.length(), |
| 339 | ParagraphStyle.class); |
| 340 | spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class); |
| 341 | |
| 342 | for (int n = spans.length - 1; n >= 0; n--) { |
| 343 | if (spans[n] instanceof AlignmentSpan) { |
| 344 | paraAlign = ((AlignmentSpan) spans[n]).getAlignment(); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | final int length = spans.length; |
| 351 | boolean useFirstLineMargin = isFirstParaLine; |
| 352 | for (int n = 0; n < length; n++) { |
| 353 | if (spans[n] instanceof LeadingMarginSpan2) { |
| 354 | int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount(); |
| 355 | int startLine = getLineForOffset(sp.getSpanStart(spans[n])); |
| 356 | if (lineNum < startLine + count) { |
| 357 | useFirstLineMargin = true; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | for (int n = 0; n < length; n++) { |
| 363 | if (spans[n] instanceof LeadingMarginSpan) { |
| 364 | LeadingMarginSpan margin = (LeadingMarginSpan) spans[n]; |
| 365 | if (dir == DIR_RIGHT_TO_LEFT) { |
| 366 | right -= margin.getLeadingMargin(useFirstLineMargin); |
| 367 | } else { |
| 368 | left += margin.getLeadingMargin(useFirstLineMargin); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (getLineContainsTab(lineNum)) { |
| 375 | tabStops = new TabStops(TAB_INCREMENT, spans); |
| 376 | } |
| 377 | |
| 378 | final Alignment align; |
| 379 | if (paraAlign == Alignment.ALIGN_LEFT) { |
| 380 | align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE; |
| 381 | } else if (paraAlign == Alignment.ALIGN_RIGHT) { |
| 382 | align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL; |
| 383 | } else { |
| 384 | align = paraAlign; |
| 385 | } |
| 386 | |
| 387 | final int indentWidth; |
| 388 | if (align == Alignment.ALIGN_NORMAL) { |
| 389 | if (dir == DIR_LEFT_TO_RIGHT) { |
| 390 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT); |
| 391 | } else { |
| 392 | indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT); |
| 393 | } |
| 394 | } else if (align == Alignment.ALIGN_OPPOSITE) { |
| 395 | if (dir == DIR_LEFT_TO_RIGHT) { |
| 396 | indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT); |
| 397 | } else { |
| 398 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT); |
| 399 | } |
| 400 | } else { // Alignment.ALIGN_CENTER |
| 401 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER); |
| 402 | } |
| 403 | |
| 404 | return right - left - indentWidth; |
| 405 | } |
| 406 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 407 | /** |
| 408 | * @hide |
| 409 | */ |
| 410 | public void drawText(Canvas canvas, int firstLine, int lastLine) { |
| 411 | int previousLineBottom = getLineTop(firstLine); |
| 412 | int previousLineEnd = getLineStart(firstLine); |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 413 | ParagraphStyle[] spans = NO_PARA_SPANS; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 414 | int spanEnd = 0; |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 415 | final TextPaint paint = mWorkPaint; |
| 416 | paint.set(mPaint); |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 417 | CharSequence buf = mText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 419 | Alignment paraAlign = mAlignment; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 420 | TabStops tabStops = null; |
| 421 | boolean tabStopsIsInitialized = false; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 422 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 423 | TextLine tl = TextLine.obtain(); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 424 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 425 | // Draw the lines, one at a time. |
| 426 | // The baseline is the top of the following line minus the current line's descent. |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 427 | for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | int start = previousLineEnd; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 429 | previousLineEnd = getLineStart(lineNum + 1); |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 430 | final boolean justify = isJustificationRequired(lineNum); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 431 | int end = getLineVisibleEnd(lineNum, start, previousLineEnd); |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 432 | paint.setHyphenEdit(getHyphen(lineNum)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | |
| 434 | int ltop = previousLineBottom; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 435 | int lbottom = getLineTop(lineNum + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | previousLineBottom = lbottom; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 437 | int lbaseline = lbottom - getLineDescent(lineNum); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 439 | int dir = getParagraphDirection(lineNum); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 440 | int left = 0; |
| 441 | int right = mWidth; |
| 442 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 443 | if (mSpannedText) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 444 | Spanned sp = (Spanned) buf; |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 445 | int textLength = buf.length(); |
| 446 | boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n'); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 447 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 448 | // New batch of paragraph styles, collect into spans array. |
| 449 | // Compute the alignment, last alignment style wins. |
| 450 | // Reset tabStops, we'll rebuild if we encounter a line with |
| 451 | // tabs. |
| 452 | // We expect paragraph spans to be relatively infrequent, use |
| 453 | // spanEnd so that we can check less frequently. Since |
| 454 | // paragraph styles ought to apply to entire paragraphs, we can |
| 455 | // just collect the ones present at the start of the paragraph. |
| 456 | // If spanEnd is before the end of the paragraph, that's not |
| 457 | // our problem. |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 458 | if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 459 | spanEnd = sp.nextSpanTransition(start, textLength, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | ParagraphStyle.class); |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 461 | spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 462 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 463 | paraAlign = mAlignment; |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 464 | for (int n = spans.length - 1; n >= 0; n--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | if (spans[n] instanceof AlignmentSpan) { |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 466 | paraAlign = ((AlignmentSpan) spans[n]).getAlignment(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | break; |
| 468 | } |
| 469 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 470 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 471 | tabStopsIsInitialized = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 473 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 474 | // Draw all leading margin spans. Adjust left or right according |
| 475 | // to the paragraph direction of the line. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | final int length = spans.length; |
Anish Athalye | ab08c6d | 2014-08-08 12:09:58 -0700 | [diff] [blame] | 477 | boolean useFirstLineMargin = isFirstParaLine; |
| 478 | for (int n = 0; n < length; n++) { |
| 479 | if (spans[n] instanceof LeadingMarginSpan2) { |
| 480 | int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount(); |
| 481 | int startLine = getLineForOffset(sp.getSpanStart(spans[n])); |
| 482 | // if there is more than one LeadingMarginSpan2, use |
| 483 | // the count that is greatest |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 484 | if (lineNum < startLine + count) { |
Anish Athalye | ab08c6d | 2014-08-08 12:09:58 -0700 | [diff] [blame] | 485 | useFirstLineMargin = true; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | for (int n = 0; n < length; n++) { |
| 491 | if (spans[n] instanceof LeadingMarginSpan) { |
| 492 | LeadingMarginSpan margin = (LeadingMarginSpan) spans[n]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | if (dir == DIR_RIGHT_TO_LEFT) { |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 494 | margin.drawLeadingMargin(canvas, paint, right, dir, ltop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | lbaseline, lbottom, buf, |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 496 | start, end, isFirstParaLine, this); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 497 | right -= margin.getLeadingMargin(useFirstLineMargin); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } else { |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 499 | margin.drawLeadingMargin(canvas, paint, left, dir, ltop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | lbaseline, lbottom, buf, |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 501 | start, end, isFirstParaLine, this); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 502 | left += margin.getLeadingMargin(useFirstLineMargin); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 508 | boolean hasTab = getLineContainsTab(lineNum); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 509 | // Can't tell if we have tabs for sure, currently |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 510 | if (hasTab && !tabStopsIsInitialized) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 511 | if (tabStops == null) { |
| 512 | tabStops = new TabStops(TAB_INCREMENT, spans); |
| 513 | } else { |
| 514 | tabStops.reset(TAB_INCREMENT, spans); |
| 515 | } |
| 516 | tabStopsIsInitialized = true; |
| 517 | } |
| 518 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 519 | // Determine whether the line aligns to normal, opposite, or center. |
| 520 | Alignment align = paraAlign; |
| 521 | if (align == Alignment.ALIGN_LEFT) { |
| 522 | align = (dir == DIR_LEFT_TO_RIGHT) ? |
| 523 | Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE; |
| 524 | } else if (align == Alignment.ALIGN_RIGHT) { |
| 525 | align = (dir == DIR_LEFT_TO_RIGHT) ? |
| 526 | Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL; |
| 527 | } |
| 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | int x; |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 530 | final int indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | if (align == Alignment.ALIGN_NORMAL) { |
| 532 | if (dir == DIR_LEFT_TO_RIGHT) { |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 533 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT); |
| 534 | x = left + indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | } else { |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 536 | indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT); |
| 537 | x = right - indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
| 539 | } else { |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 540 | int max = (int)getLineExtent(lineNum, tabStops, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | if (align == Alignment.ALIGN_OPPOSITE) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 542 | if (dir == DIR_LEFT_TO_RIGHT) { |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 543 | indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT); |
| 544 | x = right - max - indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | } else { |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 546 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT); |
| 547 | x = left - max + indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 549 | } else { // Alignment.ALIGN_CENTER |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 550 | indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 551 | max = max & ~1; |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 552 | x = ((right + left - max) >> 1) + indentWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 556 | Directions directions = getLineDirections(lineNum); |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 557 | if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) { |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 558 | // XXX: assumes there's nothing additional to be done |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 559 | canvas.drawText(buf, start, end, x, lbaseline, paint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | } else { |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 561 | tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops); |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 562 | if (justify) { |
| 563 | tl.justify(right - left - indentWidth); |
| 564 | } |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 565 | tl.draw(canvas, x, ltop, lbaseline, lbottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } |
| 567 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 568 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 569 | TextLine.recycle(tl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 573 | * @hide |
| 574 | */ |
| 575 | public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint, |
| 576 | int cursorOffsetVertical, int firstLine, int lastLine) { |
| 577 | // First, draw LineBackgroundSpans. |
| 578 | // LineBackgroundSpans know nothing about the alignment, margins, or |
| 579 | // direction of the layout or line. XXX: Should they? |
| 580 | // They are evaluated at each line. |
| 581 | if (mSpannedText) { |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 582 | if (mLineBackgroundSpans == null) { |
| 583 | mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class); |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 584 | } |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 585 | |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 586 | Spanned buffer = (Spanned) mText; |
| 587 | int textLength = buffer.length(); |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 588 | mLineBackgroundSpans.init(buffer, 0, textLength); |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 589 | |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 590 | if (mLineBackgroundSpans.numberOfSpans > 0) { |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 591 | int previousLineBottom = getLineTop(firstLine); |
| 592 | int previousLineEnd = getLineStart(firstLine); |
| 593 | ParagraphStyle[] spans = NO_PARA_SPANS; |
| 594 | int spansLength = 0; |
| 595 | TextPaint paint = mPaint; |
| 596 | int spanEnd = 0; |
| 597 | final int width = mWidth; |
| 598 | for (int i = firstLine; i <= lastLine; i++) { |
| 599 | int start = previousLineEnd; |
| 600 | int end = getLineStart(i + 1); |
| 601 | previousLineEnd = end; |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 602 | |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 603 | int ltop = previousLineBottom; |
| 604 | int lbottom = getLineTop(i + 1); |
| 605 | previousLineBottom = lbottom; |
| 606 | int lbaseline = lbottom - getLineDescent(i); |
| 607 | |
| 608 | if (start >= spanEnd) { |
| 609 | // These should be infrequent, so we'll use this so that |
| 610 | // we don't have to check as often. |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 611 | spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength); |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 612 | // All LineBackgroundSpans on a line contribute to its background. |
| 613 | spansLength = 0; |
| 614 | // Duplication of the logic of getParagraphSpans |
| 615 | if (start != end || start == 0) { |
| 616 | // Equivalent to a getSpans(start, end), but filling the 'spans' local |
| 617 | // array instead to reduce memory allocation |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 618 | for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) { |
| 619 | // equal test is valid since both intervals are not empty by |
| 620 | // construction |
| 621 | if (mLineBackgroundSpans.spanStarts[j] >= end || |
| 622 | mLineBackgroundSpans.spanEnds[j] <= start) continue; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 623 | spans = GrowingArrayUtils.append( |
| 624 | spans, spansLength, mLineBackgroundSpans.spans[j]); |
| 625 | spansLength++; |
Gilles Debunne | 60e3b3f | 2012-03-13 11:26:05 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | for (int n = 0; n < spansLength; n++) { |
| 631 | LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n]; |
| 632 | lineBackgroundSpan.drawBackground(canvas, paint, 0, width, |
| 633 | ltop, lbaseline, lbottom, |
| 634 | buffer, start, end, i); |
| 635 | } |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 636 | } |
| 637 | } |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 638 | mLineBackgroundSpans.recycle(); |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // There can be a highlight even without spans if we are drawing |
| 642 | // a non-spanned transformation of a spanned editing buffer. |
| 643 | if (highlight != null) { |
| 644 | if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical); |
| 645 | canvas.drawPath(highlight, highlightPaint); |
| 646 | if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * @param canvas |
| 652 | * @return The range of lines that need to be drawn, possibly empty. |
| 653 | * @hide |
| 654 | */ |
| 655 | public long getLineRangeForDraw(Canvas canvas) { |
| 656 | int dtop, dbottom; |
| 657 | |
| 658 | synchronized (sTempRect) { |
| 659 | if (!canvas.getClipBounds(sTempRect)) { |
| 660 | // Negative range end used as a special flag |
| 661 | return TextUtils.packRangeInLong(0, -1); |
| 662 | } |
| 663 | |
| 664 | dtop = sTempRect.top; |
| 665 | dbottom = sTempRect.bottom; |
| 666 | } |
| 667 | |
| 668 | final int top = Math.max(dtop, 0); |
| 669 | final int bottom = Math.min(getLineTop(getLineCount()), dbottom); |
| 670 | |
Gilles Debunne | 2fba338 | 2012-06-11 17:46:24 -0700 | [diff] [blame] | 671 | if (top >= bottom) return TextUtils.packRangeInLong(0, -1); |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 672 | return TextUtils.packRangeInLong(getLineForVertical(top), getLineForVertical(bottom)); |
| 673 | } |
| 674 | |
| 675 | /** |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 676 | * Return the start position of the line, given the left and right bounds |
| 677 | * of the margins. |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 678 | * |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 679 | * @param line the line index |
| 680 | * @param left the left bounds (0, or leading margin if ltr para) |
| 681 | * @param right the right bounds (width, minus leading margin if rtl para) |
| 682 | * @return the start position of the line (to right of line if rtl para) |
| 683 | */ |
| 684 | private int getLineStartPos(int line, int left, int right) { |
| 685 | // Adjust the point at which to start rendering depending on the |
| 686 | // alignment of the paragraph. |
| 687 | Alignment align = getParagraphAlignment(line); |
| 688 | int dir = getParagraphDirection(line); |
| 689 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 690 | if (align == Alignment.ALIGN_LEFT) { |
Fabrice Di Meglio | db24f0a | 2012-10-03 17:17:14 -0700 | [diff] [blame] | 691 | align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE; |
| 692 | } else if (align == Alignment.ALIGN_RIGHT) { |
| 693 | align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL; |
| 694 | } |
| 695 | |
| 696 | int x; |
| 697 | if (align == Alignment.ALIGN_NORMAL) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 698 | if (dir == DIR_LEFT_TO_RIGHT) { |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 699 | x = left + getIndentAdjust(line, Alignment.ALIGN_LEFT); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 700 | } else { |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 701 | x = right + getIndentAdjust(line, Alignment.ALIGN_RIGHT); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 702 | } |
| 703 | } else { |
| 704 | TabStops tabStops = null; |
| 705 | if (mSpannedText && getLineContainsTab(line)) { |
| 706 | Spanned spanned = (Spanned) mText; |
| 707 | int start = getLineStart(line); |
| 708 | int spanEnd = spanned.nextSpanTransition(start, spanned.length(), |
| 709 | TabStopSpan.class); |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 710 | TabStopSpan[] tabSpans = getParagraphSpans(spanned, start, spanEnd, |
| 711 | TabStopSpan.class); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 712 | if (tabSpans.length > 0) { |
| 713 | tabStops = new TabStops(TAB_INCREMENT, tabSpans); |
| 714 | } |
| 715 | } |
| 716 | int max = (int)getLineExtent(line, tabStops, false); |
Fabrice Di Meglio | db24f0a | 2012-10-03 17:17:14 -0700 | [diff] [blame] | 717 | if (align == Alignment.ALIGN_OPPOSITE) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 718 | if (dir == DIR_LEFT_TO_RIGHT) { |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 719 | x = right - max + getIndentAdjust(line, Alignment.ALIGN_RIGHT); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 720 | } else { |
Fabrice Di Meglio | db24f0a | 2012-10-03 17:17:14 -0700 | [diff] [blame] | 721 | // max is negative here |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 722 | x = left - max + getIndentAdjust(line, Alignment.ALIGN_LEFT); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 723 | } |
| 724 | } else { // Alignment.ALIGN_CENTER |
| 725 | max = max & ~1; |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 726 | x = (left + right - max) >> 1 + getIndentAdjust(line, Alignment.ALIGN_CENTER); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | return x; |
| 730 | } |
| 731 | |
| 732 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | * Return the text that is displayed by this Layout. |
| 734 | */ |
| 735 | public final CharSequence getText() { |
| 736 | return mText; |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Return the base Paint properties for this layout. |
| 741 | * Do NOT change the paint, which may result in funny |
| 742 | * drawing for this layout. |
| 743 | */ |
| 744 | public final TextPaint getPaint() { |
| 745 | return mPaint; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Return the width of this layout. |
| 750 | */ |
| 751 | public final int getWidth() { |
| 752 | return mWidth; |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * Return the width to which this Layout is ellipsizing, or |
| 757 | * {@link #getWidth} if it is not doing anything special. |
| 758 | */ |
| 759 | public int getEllipsizedWidth() { |
| 760 | return mWidth; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Increase the width of this layout to the specified width. |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 765 | * Be careful to use this only when you know it is appropriate— |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | * it does not cause the text to reflow to use the full new width. |
| 767 | */ |
| 768 | public final void increaseWidthTo(int wid) { |
| 769 | if (wid < mWidth) { |
| 770 | throw new RuntimeException("attempted to reduce Layout width"); |
| 771 | } |
| 772 | |
| 773 | mWidth = wid; |
| 774 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | /** |
| 777 | * Return the total height of this layout. |
| 778 | */ |
| 779 | public int getHeight() { |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 780 | return getLineTop(getLineCount()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 784 | * Return the total height of this layout. |
| 785 | * |
| 786 | * @param cap if true and max lines is set, returns the height of the layout at the max lines. |
| 787 | * |
| 788 | * @hide |
| 789 | */ |
| 790 | public int getHeight(boolean cap) { |
| 791 | return getHeight(); |
| 792 | } |
| 793 | |
| 794 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | * Return the base alignment of this layout. |
| 796 | */ |
| 797 | public final Alignment getAlignment() { |
| 798 | return mAlignment; |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Return what the text height is multiplied by to get the line height. |
| 803 | */ |
| 804 | public final float getSpacingMultiplier() { |
| 805 | return mSpacingMult; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Return the number of units of leading that are added to each line. |
| 810 | */ |
| 811 | public final float getSpacingAdd() { |
| 812 | return mSpacingAdd; |
| 813 | } |
| 814 | |
| 815 | /** |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 816 | * Return the heuristic used to determine paragraph text direction. |
| 817 | * @hide |
| 818 | */ |
| 819 | public final TextDirectionHeuristic getTextDirectionHeuristic() { |
| 820 | return mTextDir; |
| 821 | } |
| 822 | |
| 823 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | * Return the number of lines of text in this layout. |
| 825 | */ |
| 826 | public abstract int getLineCount(); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | /** |
| 829 | * Return the baseline for the specified line (0…getLineCount() - 1) |
| 830 | * If bounds is not null, return the top, left, right, bottom extents |
| 831 | * of the specified line in it. |
| 832 | * @param line which line to examine (0..getLineCount() - 1) |
| 833 | * @param bounds Optional. If not null, it returns the extent of the line |
| 834 | * @return the Y-coordinate of the baseline |
| 835 | */ |
| 836 | public int getLineBounds(int line, Rect bounds) { |
| 837 | if (bounds != null) { |
| 838 | bounds.left = 0; // ??? |
| 839 | bounds.top = getLineTop(line); |
| 840 | bounds.right = mWidth; // ??? |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 841 | bounds.bottom = getLineTop(line + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | } |
| 843 | return getLineBaseline(line); |
| 844 | } |
| 845 | |
| 846 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 847 | * Return the vertical position of the top of the specified line |
| 848 | * (0…getLineCount()). |
| 849 | * If the specified line is equal to the line count, returns the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | * bottom of the last line. |
| 851 | */ |
| 852 | public abstract int getLineTop(int line); |
| 853 | |
| 854 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 855 | * Return the descent of the specified line(0…getLineCount() - 1). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | */ |
| 857 | public abstract int getLineDescent(int line); |
| 858 | |
| 859 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 860 | * Return the text offset of the beginning of the specified line ( |
| 861 | * 0…getLineCount()). If the specified line is equal to the line |
| 862 | * count, returns the length of the text. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | */ |
| 864 | public abstract int getLineStart(int line); |
| 865 | |
| 866 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 867 | * Returns the primary directionality of the paragraph containing the |
| 868 | * specified line, either 1 for left-to-right lines, or -1 for right-to-left |
| 869 | * lines (see {@link #DIR_LEFT_TO_RIGHT}, {@link #DIR_RIGHT_TO_LEFT}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | */ |
| 871 | public abstract int getParagraphDirection(int line); |
| 872 | |
| 873 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 874 | * Returns whether the specified line contains one or more |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 875 | * characters that need to be handled specially, like tabs. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | */ |
| 877 | public abstract boolean getLineContainsTab(int line); |
| 878 | |
| 879 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 880 | * Returns the directional run information for the specified line. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | * The array alternates counts of characters in left-to-right |
| 882 | * and right-to-left segments of the line. |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 883 | * |
| 884 | * <p>NOTE: this is inadequate to support bidirectional text, and will change. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | */ |
| 886 | public abstract Directions getLineDirections(int line); |
| 887 | |
| 888 | /** |
| 889 | * Returns the (negative) number of extra pixels of ascent padding in the |
| 890 | * top line of the Layout. |
| 891 | */ |
| 892 | public abstract int getTopPadding(); |
| 893 | |
| 894 | /** |
| 895 | * Returns the number of extra pixels of descent padding in the |
| 896 | * bottom line of the Layout. |
| 897 | */ |
| 898 | public abstract int getBottomPadding(); |
| 899 | |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 900 | /** |
| 901 | * Returns the hyphen edit for a line. |
| 902 | * |
| 903 | * @hide |
| 904 | */ |
| 905 | public int getHyphen(int line) { |
| 906 | return 0; |
| 907 | } |
| 908 | |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 909 | /** |
| 910 | * Returns the left indent for a line. |
| 911 | * |
| 912 | * @hide |
| 913 | */ |
| 914 | public int getIndentAdjust(int line, Alignment alignment) { |
| 915 | return 0; |
| 916 | } |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 917 | |
| 918 | /** |
| 919 | * Returns true if the character at offset and the preceding character |
| 920 | * are at different run levels (and thus there's a split caret). |
| 921 | * @param offset the offset |
| 922 | * @return true if at a level boundary |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 923 | * @hide |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 924 | */ |
Gilles Debunne | f75c97e | 2011-02-10 16:09:53 -0800 | [diff] [blame] | 925 | public boolean isLevelBoundary(int offset) { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 926 | int line = getLineForOffset(offset); |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 927 | Directions dirs = getLineDirections(line); |
| 928 | if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) { |
| 929 | return false; |
| 930 | } |
| 931 | |
| 932 | int[] runs = dirs.mDirections; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 933 | int lineStart = getLineStart(line); |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 934 | int lineEnd = getLineEnd(line); |
| 935 | if (offset == lineStart || offset == lineEnd) { |
| 936 | int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1; |
| 937 | int runIndex = offset == lineStart ? 0 : runs.length - 2; |
| 938 | return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel; |
| 939 | } |
| 940 | |
| 941 | offset -= lineStart; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 942 | for (int i = 0; i < runs.length; i += 2) { |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 943 | if (offset == runs[i]) { |
| 944 | return true; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 945 | } |
| 946 | } |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 947 | return false; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 950 | /** |
| 951 | * Returns true if the character at offset is right to left (RTL). |
| 952 | * @param offset the offset |
| 953 | * @return true if the character is RTL, false if it is LTR |
| 954 | */ |
| 955 | public boolean isRtlCharAt(int offset) { |
| 956 | int line = getLineForOffset(offset); |
| 957 | Directions dirs = getLineDirections(line); |
| 958 | if (dirs == DIRS_ALL_LEFT_TO_RIGHT) { |
| 959 | return false; |
| 960 | } |
| 961 | if (dirs == DIRS_ALL_RIGHT_TO_LEFT) { |
| 962 | return true; |
| 963 | } |
| 964 | int[] runs = dirs.mDirections; |
| 965 | int lineStart = getLineStart(line); |
| 966 | for (int i = 0; i < runs.length; i += 2) { |
Raph Levien | 8750620 | 2014-09-04 12:47:03 -0700 | [diff] [blame] | 967 | int start = lineStart + runs[i]; |
| 968 | int limit = start + (runs[i+1] & RUN_LENGTH_MASK); |
| 969 | if (offset >= start && offset < limit) { |
Fabrice Di Meglio | 34d2eba | 2011-08-31 19:46:15 -0700 | [diff] [blame] | 970 | int level = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; |
| 971 | return ((level & 1) != 0); |
| 972 | } |
| 973 | } |
| 974 | // Should happen only if the offset is "out of bounds" |
| 975 | return false; |
| 976 | } |
| 977 | |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 978 | /** |
| 979 | * Returns the range of the run that the character at offset belongs to. |
| 980 | * @param offset the offset |
| 981 | * @return The range of the run |
| 982 | * @hide |
| 983 | */ |
| 984 | public long getRunRange(int offset) { |
| 985 | int line = getLineForOffset(offset); |
| 986 | Directions dirs = getLineDirections(line); |
| 987 | if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) { |
| 988 | return TextUtils.packRangeInLong(0, getLineEnd(line)); |
| 989 | } |
| 990 | int[] runs = dirs.mDirections; |
| 991 | int lineStart = getLineStart(line); |
| 992 | for (int i = 0; i < runs.length; i += 2) { |
| 993 | int start = lineStart + runs[i]; |
| 994 | int limit = start + (runs[i+1] & RUN_LENGTH_MASK); |
| 995 | if (offset >= start && offset < limit) { |
| 996 | return TextUtils.packRangeInLong(start, limit); |
| 997 | } |
| 998 | } |
| 999 | // Should happen only if the offset is "out of bounds" |
| 1000 | return TextUtils.packRangeInLong(0, getLineEnd(line)); |
| 1001 | } |
| 1002 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1003 | private boolean primaryIsTrailingPrevious(int offset) { |
| 1004 | int line = getLineForOffset(offset); |
| 1005 | int lineStart = getLineStart(line); |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 1006 | int lineEnd = getLineEnd(line); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1007 | int[] runs = getLineDirections(line).mDirections; |
| 1008 | |
| 1009 | int levelAt = -1; |
| 1010 | for (int i = 0; i < runs.length; i += 2) { |
| 1011 | int start = lineStart + runs[i]; |
| 1012 | int limit = start + (runs[i+1] & RUN_LENGTH_MASK); |
| 1013 | if (limit > lineEnd) { |
| 1014 | limit = lineEnd; |
| 1015 | } |
| 1016 | if (offset >= start && offset < limit) { |
| 1017 | if (offset > start) { |
| 1018 | // Previous character is at same level, so don't use trailing. |
| 1019 | return false; |
| 1020 | } |
| 1021 | levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; |
| 1022 | break; |
| 1023 | } |
| 1024 | } |
| 1025 | if (levelAt == -1) { |
| 1026 | // Offset was limit of line. |
| 1027 | levelAt = getParagraphDirection(line) == 1 ? 0 : 1; |
| 1028 | } |
| 1029 | |
| 1030 | // At level boundary, check previous level. |
| 1031 | int levelBefore = -1; |
| 1032 | if (offset == lineStart) { |
| 1033 | levelBefore = getParagraphDirection(line) == 1 ? 0 : 1; |
| 1034 | } else { |
| 1035 | offset -= 1; |
| 1036 | for (int i = 0; i < runs.length; i += 2) { |
| 1037 | int start = lineStart + runs[i]; |
| 1038 | int limit = start + (runs[i+1] & RUN_LENGTH_MASK); |
| 1039 | if (limit > lineEnd) { |
| 1040 | limit = lineEnd; |
| 1041 | } |
| 1042 | if (offset >= start && offset < limit) { |
| 1043 | levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return levelBefore < levelAt; |
| 1050 | } |
| 1051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | /** |
| 1053 | * Get the primary horizontal position for the specified text offset. |
| 1054 | * This is the location where a new character would be inserted in |
| 1055 | * the paragraph's primary direction. |
| 1056 | */ |
| 1057 | public float getPrimaryHorizontal(int offset) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1058 | return getPrimaryHorizontal(offset, false /* not clamped */); |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Get the primary horizontal position for the specified text offset, but |
| 1063 | * optionally clamp it so that it doesn't exceed the width of the layout. |
| 1064 | * @hide |
| 1065 | */ |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1066 | public float getPrimaryHorizontal(int offset, boolean clamped) { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1067 | boolean trailing = primaryIsTrailingPrevious(offset); |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1068 | return getHorizontal(offset, trailing, clamped); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * Get the secondary horizontal position for the specified text offset. |
| 1073 | * This is the location where a new character would be inserted in |
| 1074 | * the direction other than the paragraph's primary direction. |
| 1075 | */ |
| 1076 | public float getSecondaryHorizontal(int offset) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1077 | return getSecondaryHorizontal(offset, false /* not clamped */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1080 | /** |
| 1081 | * Get the secondary horizontal position for the specified text offset, but |
| 1082 | * optionally clamp it so that it doesn't exceed the width of the layout. |
| 1083 | * @hide |
| 1084 | */ |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1085 | public float getSecondaryHorizontal(int offset, boolean clamped) { |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1086 | boolean trailing = primaryIsTrailingPrevious(offset); |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1087 | return getHorizontal(offset, !trailing, clamped); |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1090 | private float getHorizontal(int offset, boolean primary) { |
| 1091 | return primary ? getPrimaryHorizontal(offset) : getSecondaryHorizontal(offset); |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 1092 | } |
| 1093 | |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1094 | private float getHorizontal(int offset, boolean trailing, boolean clamped) { |
| 1095 | int line = getLineForOffset(offset); |
| 1096 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1097 | return getHorizontal(offset, trailing, line, clamped); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1100 | private float getHorizontal(int offset, boolean trailing, int line, boolean clamped) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | int start = getLineStart(line); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1102 | int end = getLineEnd(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | int dir = getParagraphDirection(line); |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 1104 | boolean hasTab = getLineContainsTab(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | Directions directions = getLineDirections(line); |
| 1106 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1107 | TabStops tabStops = null; |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 1108 | if (hasTab && mText instanceof Spanned) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1109 | // Just checking this line should be good enough, tabs should be |
| 1110 | // consistent across all lines in a paragraph. |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 1111 | TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1112 | if (tabs.length > 0) { |
| 1113 | tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse |
| 1114 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1117 | TextLine tl = TextLine.obtain(); |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 1118 | tl.set(mPaint, mText, start, end, dir, directions, hasTab, tabStops); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1119 | float wid = tl.measure(offset - start, trailing, null); |
| 1120 | TextLine.recycle(tl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1122 | if (clamped && wid > mWidth) { |
| 1123 | wid = mWidth; |
| 1124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | int left = getParagraphLeft(line); |
| 1126 | int right = getParagraphRight(line); |
| 1127 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1128 | return getLineStartPos(line, left, right) + wid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Get the leftmost position that should be exposed for horizontal |
| 1133 | * scrolling on the specified line. |
| 1134 | */ |
| 1135 | public float getLineLeft(int line) { |
| 1136 | int dir = getParagraphDirection(line); |
| 1137 | Alignment align = getParagraphAlignment(line); |
| 1138 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 1139 | if (align == Alignment.ALIGN_LEFT) { |
| 1140 | return 0; |
| 1141 | } else if (align == Alignment.ALIGN_NORMAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | if (dir == DIR_RIGHT_TO_LEFT) |
| 1143 | return getParagraphRight(line) - getLineMax(line); |
| 1144 | else |
| 1145 | return 0; |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 1146 | } else if (align == Alignment.ALIGN_RIGHT) { |
| 1147 | return mWidth - getLineMax(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } else if (align == Alignment.ALIGN_OPPOSITE) { |
| 1149 | if (dir == DIR_RIGHT_TO_LEFT) |
| 1150 | return 0; |
| 1151 | else |
| 1152 | return mWidth - getLineMax(line); |
| 1153 | } else { /* align == Alignment.ALIGN_CENTER */ |
| 1154 | int left = getParagraphLeft(line); |
| 1155 | int right = getParagraphRight(line); |
| 1156 | int max = ((int) getLineMax(line)) & ~1; |
| 1157 | |
| 1158 | return left + ((right - left) - max) / 2; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * Get the rightmost position that should be exposed for horizontal |
| 1164 | * scrolling on the specified line. |
| 1165 | */ |
| 1166 | public float getLineRight(int line) { |
| 1167 | int dir = getParagraphDirection(line); |
| 1168 | Alignment align = getParagraphAlignment(line); |
| 1169 | |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 1170 | if (align == Alignment.ALIGN_LEFT) { |
| 1171 | return getParagraphLeft(line) + getLineMax(line); |
| 1172 | } else if (align == Alignment.ALIGN_NORMAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | if (dir == DIR_RIGHT_TO_LEFT) |
| 1174 | return mWidth; |
| 1175 | else |
| 1176 | return getParagraphLeft(line) + getLineMax(line); |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 1177 | } else if (align == Alignment.ALIGN_RIGHT) { |
| 1178 | return mWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | } else if (align == Alignment.ALIGN_OPPOSITE) { |
| 1180 | if (dir == DIR_RIGHT_TO_LEFT) |
| 1181 | return getLineMax(line); |
| 1182 | else |
| 1183 | return mWidth; |
| 1184 | } else { /* align == Alignment.ALIGN_CENTER */ |
| 1185 | int left = getParagraphLeft(line); |
| 1186 | int right = getParagraphRight(line); |
| 1187 | int max = ((int) getLineMax(line)) & ~1; |
| 1188 | |
| 1189 | return right - ((right - left) - max) / 2; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /** |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1194 | * Gets the unsigned horizontal extent of the specified line, including |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1195 | * leading margin indent, but excluding trailing whitespace. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | */ |
| 1197 | public float getLineMax(int line) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1198 | float margin = getParagraphLeadingMargin(line); |
| 1199 | float signedExtent = getLineExtent(line, false); |
Anish Athalye | c14b3ad | 2014-07-24 18:03:21 -0700 | [diff] [blame] | 1200 | return margin + (signedExtent >= 0 ? signedExtent : -signedExtent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /** |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1204 | * Gets the unsigned horizontal extent of the specified line, including |
| 1205 | * leading margin indent and trailing whitespace. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | */ |
| 1207 | public float getLineWidth(int line) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1208 | float margin = getParagraphLeadingMargin(line); |
| 1209 | float signedExtent = getLineExtent(line, true); |
Anish Athalye | c14b3ad | 2014-07-24 18:03:21 -0700 | [diff] [blame] | 1210 | return margin + (signedExtent >= 0 ? signedExtent : -signedExtent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1213 | /** |
| 1214 | * Like {@link #getLineExtent(int,TabStops,boolean)} but determines the |
| 1215 | * tab stops instead of using the ones passed in. |
| 1216 | * @param line the index of the line |
| 1217 | * @param full whether to include trailing whitespace |
| 1218 | * @return the extent of the line |
| 1219 | */ |
| 1220 | private float getLineExtent(int line, boolean full) { |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1221 | final int start = getLineStart(line); |
| 1222 | final int end = full ? getLineEnd(line) : getLineVisibleEnd(line); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1223 | |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1224 | final boolean hasTabs = getLineContainsTab(line); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1225 | TabStops tabStops = null; |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 1226 | if (hasTabs && mText instanceof Spanned) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1227 | // Just checking this line should be good enough, tabs should be |
| 1228 | // consistent across all lines in a paragraph. |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 1229 | TabStopSpan[] tabs = getParagraphSpans((Spanned) mText, start, end, TabStopSpan.class); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1230 | if (tabs.length > 0) { |
| 1231 | tabStops = new TabStops(TAB_INCREMENT, tabs); // XXX should reuse |
| 1232 | } |
| 1233 | } |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1234 | final Directions directions = getLineDirections(line); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1235 | // Returned directions can actually be null |
| 1236 | if (directions == null) { |
| 1237 | return 0f; |
| 1238 | } |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1239 | final int dir = getParagraphDirection(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1241 | final TextLine tl = TextLine.obtain(); |
| 1242 | final TextPaint paint = mWorkPaint; |
| 1243 | paint.set(mPaint); |
| 1244 | paint.setHyphenEdit(getHyphen(line)); |
Roozbeh Pournader | 53aba29 | 2017-08-25 15:53:33 -0700 | [diff] [blame] | 1245 | tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops); |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 1246 | if (isJustificationRequired(line)) { |
| 1247 | tl.justify(getJustifyWidth(line)); |
| 1248 | } |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1249 | final float width = tl.metrics(null); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1250 | TextLine.recycle(tl); |
| 1251 | return width; |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * Returns the signed horizontal extent of the specified line, excluding |
| 1256 | * leading margin. If full is false, excludes trailing whitespace. |
| 1257 | * @param line the index of the line |
| 1258 | * @param tabStops the tab stops, can be null if we know they're not used. |
| 1259 | * @param full whether to include trailing whitespace |
| 1260 | * @return the extent of the text on this line |
| 1261 | */ |
| 1262 | private float getLineExtent(int line, TabStops tabStops, boolean full) { |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1263 | final int start = getLineStart(line); |
| 1264 | final int end = full ? getLineEnd(line) : getLineVisibleEnd(line); |
| 1265 | final boolean hasTabs = getLineContainsTab(line); |
| 1266 | final Directions directions = getLineDirections(line); |
| 1267 | final int dir = getParagraphDirection(line); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1268 | |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1269 | final TextLine tl = TextLine.obtain(); |
| 1270 | final TextPaint paint = mWorkPaint; |
| 1271 | paint.set(mPaint); |
| 1272 | paint.setHyphenEdit(getHyphen(line)); |
Roozbeh Pournader | 53aba29 | 2017-08-25 15:53:33 -0700 | [diff] [blame] | 1273 | tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops); |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 1274 | if (isJustificationRequired(line)) { |
| 1275 | tl.justify(getJustifyWidth(line)); |
| 1276 | } |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 1277 | final float width = tl.metrics(null); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1278 | TextLine.recycle(tl); |
| 1279 | return width; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * Get the line number corresponding to the specified vertical position. |
| 1284 | * If you ask for a position above 0, you get 0; if you ask for a position |
| 1285 | * below the bottom of the text, you get the last line. |
| 1286 | */ |
| 1287 | // FIXME: It may be faster to do a linear search for layouts without many lines. |
| 1288 | public int getLineForVertical(int vertical) { |
| 1289 | int high = getLineCount(), low = -1, guess; |
| 1290 | |
| 1291 | while (high - low > 1) { |
| 1292 | guess = (high + low) / 2; |
| 1293 | |
| 1294 | if (getLineTop(guess) > vertical) |
| 1295 | high = guess; |
| 1296 | else |
| 1297 | low = guess; |
| 1298 | } |
| 1299 | |
| 1300 | if (low < 0) |
| 1301 | return 0; |
| 1302 | else |
| 1303 | return low; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * Get the line number on which the specified text offset appears. |
| 1308 | * If you ask for a position before 0, you get 0; if you ask for a position |
| 1309 | * beyond the end of the text, you get the last line. |
| 1310 | */ |
| 1311 | public int getLineForOffset(int offset) { |
| 1312 | int high = getLineCount(), low = -1, guess; |
| 1313 | |
| 1314 | while (high - low > 1) { |
| 1315 | guess = (high + low) / 2; |
| 1316 | |
| 1317 | if (getLineStart(guess) > offset) |
| 1318 | high = guess; |
| 1319 | else |
| 1320 | low = guess; |
| 1321 | } |
| 1322 | |
Keisuke Kuroyanagi | 7c263dd | 2017-01-12 19:24:18 +0900 | [diff] [blame] | 1323 | if (low < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | return 0; |
Keisuke Kuroyanagi | 7c263dd | 2017-01-12 19:24:18 +0900 | [diff] [blame] | 1325 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | return low; |
Keisuke Kuroyanagi | 7c263dd | 2017-01-12 19:24:18 +0900 | [diff] [blame] | 1327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /** |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1331 | * Get the character offset on the specified line whose position is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | * closest to the specified horizontal position. |
| 1333 | */ |
| 1334 | public int getOffsetForHorizontal(int line, float horiz) { |
Keisuke Kuroyanagi | f0bb87b7 | 2016-02-08 23:52:55 +0900 | [diff] [blame] | 1335 | return getOffsetForHorizontal(line, horiz, true); |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Get the character offset on the specified line whose position is |
| 1340 | * closest to the specified horizontal position. |
| 1341 | * |
| 1342 | * @param line the line used to find the closest offset |
| 1343 | * @param horiz the horizontal position used to find the closest offset |
| 1344 | * @param primary whether to use the primary position or secondary position to find the offset |
| 1345 | * |
| 1346 | * @hide |
| 1347 | */ |
| 1348 | public int getOffsetForHorizontal(int line, float horiz, boolean primary) { |
Raph Levien | edb27f1 | 2015-06-01 14:34:47 -0700 | [diff] [blame] | 1349 | // TODO: use Paint.getOffsetForAdvance to avoid binary search |
Keisuke Kuroyanagi | 00ad16d | 2015-08-27 18:15:48 +0900 | [diff] [blame] | 1350 | final int lineEndOffset = getLineEnd(line); |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1351 | final int lineStartOffset = getLineStart(line); |
| 1352 | |
| 1353 | Directions dirs = getLineDirections(line); |
| 1354 | |
| 1355 | TextLine tl = TextLine.obtain(); |
| 1356 | // XXX: we don't care about tabs as we just use TextLine#getOffsetToLeftRightOf here. |
| 1357 | tl.set(mPaint, mText, lineStartOffset, lineEndOffset, getParagraphDirection(line), dirs, |
| 1358 | false, null); |
| 1359 | |
Keisuke Kuroyanagi | 00ad16d | 2015-08-27 18:15:48 +0900 | [diff] [blame] | 1360 | final int max; |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1361 | if (line == getLineCount() - 1) { |
| 1362 | max = lineEndOffset; |
| 1363 | } else { |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1364 | max = tl.getOffsetToLeftRightOf(lineEndOffset - lineStartOffset, |
| 1365 | !isRtlCharAt(lineEndOffset - 1)) + lineStartOffset; |
Keisuke Kuroyanagi | 00ad16d | 2015-08-27 18:15:48 +0900 | [diff] [blame] | 1366 | } |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1367 | int best = lineStartOffset; |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1368 | float bestdist = Math.abs(getHorizontal(best, primary) - horiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1370 | for (int i = 0; i < dirs.mDirections.length; i += 2) { |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1371 | int here = lineStartOffset + dirs.mDirections[i]; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1372 | int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK); |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1373 | boolean isRtl = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0; |
| 1374 | int swap = isRtl ? -1 : 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | |
| 1376 | if (there > max) |
| 1377 | there = max; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | int high = there - 1 + 1, low = here + 1 - 1, guess; |
| 1379 | |
| 1380 | while (high - low > 1) { |
| 1381 | guess = (high + low) / 2; |
| 1382 | int adguess = getOffsetAtStartOf(guess); |
| 1383 | |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1384 | if (getHorizontal(adguess, primary) * swap >= horiz * swap) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | high = guess; |
Keisuke Kuroyanagi | 7c263dd | 2017-01-12 19:24:18 +0900 | [diff] [blame] | 1386 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | low = guess; |
Keisuke Kuroyanagi | 7c263dd | 2017-01-12 19:24:18 +0900 | [diff] [blame] | 1388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | if (low < here + 1) |
| 1392 | low = here + 1; |
| 1393 | |
| 1394 | if (low < there) { |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1395 | int aft = tl.getOffsetToLeftRightOf(low - lineStartOffset, isRtl) + lineStartOffset; |
| 1396 | low = tl.getOffsetToLeftRightOf(aft - lineStartOffset, !isRtl) + lineStartOffset; |
| 1397 | if (low >= here && low < there) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1398 | float dist = Math.abs(getHorizontal(low, primary) - horiz); |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1399 | if (aft < there) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1400 | float other = Math.abs(getHorizontal(aft, primary) - horiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1402 | if (other < dist) { |
| 1403 | dist = other; |
| 1404 | low = aft; |
| 1405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1408 | if (dist < bestdist) { |
| 1409 | bestdist = dist; |
| 1410 | best = low; |
| 1411 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1415 | float dist = Math.abs(getHorizontal(here, primary) - horiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | |
| 1417 | if (dist < bestdist) { |
| 1418 | bestdist = dist; |
| 1419 | best = here; |
| 1420 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1423 | float dist = Math.abs(getHorizontal(max, primary) - horiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | |
Raph Levien | 373b7a8 | 2013-09-20 15:11:52 -0700 | [diff] [blame] | 1425 | if (dist <= bestdist) { |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1426 | bestdist = dist; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | best = max; |
| 1428 | } |
| 1429 | |
Keisuke Kuroyanagi | 5bff01d | 2016-01-08 19:55:17 +0900 | [diff] [blame] | 1430 | TextLine.recycle(tl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | return best; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * Return the text offset after the last character on the specified line. |
| 1436 | */ |
| 1437 | public final int getLineEnd(int line) { |
| 1438 | return getLineStart(line + 1); |
| 1439 | } |
| 1440 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1441 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | * Return the text offset after the last visible character (so whitespace |
| 1443 | * is not counted) on the specified line. |
| 1444 | */ |
| 1445 | public int getLineVisibleEnd(int line) { |
| 1446 | return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1)); |
| 1447 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1448 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | private int getLineVisibleEnd(int line, int start, int end) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | CharSequence text = mText; |
| 1451 | char ch; |
| 1452 | if (line == getLineCount() - 1) { |
| 1453 | return end; |
| 1454 | } |
| 1455 | |
| 1456 | for (; end > start; end--) { |
| 1457 | ch = text.charAt(end - 1); |
| 1458 | |
| 1459 | if (ch == '\n') { |
| 1460 | return end - 1; |
| 1461 | } |
| 1462 | |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 1463 | if (!TextLine.isLineEndSpace(ch)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | break; |
| 1465 | } |
| 1466 | |
| 1467 | } |
| 1468 | |
| 1469 | return end; |
| 1470 | } |
| 1471 | |
| 1472 | /** |
| 1473 | * Return the vertical position of the bottom of the specified line. |
| 1474 | */ |
| 1475 | public final int getLineBottom(int line) { |
| 1476 | return getLineTop(line + 1); |
| 1477 | } |
| 1478 | |
| 1479 | /** |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1480 | * Return the vertical position of the bottom of the specified line without the line spacing |
| 1481 | * added. |
| 1482 | * |
| 1483 | * @hide |
| 1484 | */ |
| 1485 | public final int getLineBottomWithoutSpacing(int line) { |
| 1486 | return getLineTop(line + 1) - getLineExtra(line); |
| 1487 | } |
| 1488 | |
| 1489 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | * Return the vertical position of the baseline of the specified line. |
| 1491 | */ |
| 1492 | public final int getLineBaseline(int line) { |
| 1493 | // getLineTop(line+1) == getLineTop(line) |
| 1494 | return getLineTop(line+1) - getLineDescent(line); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * Get the ascent of the text on the specified line. |
| 1499 | * The return value is negative to match the Paint.ascent() convention. |
| 1500 | */ |
| 1501 | public final int getLineAscent(int line) { |
| 1502 | // getLineTop(line+1) - getLineDescent(line) == getLineBaseLine(line) |
| 1503 | return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line)); |
| 1504 | } |
| 1505 | |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1506 | /** |
| 1507 | * Return the extra space added as a result of line spacing attributes |
| 1508 | * {@link #getSpacingAdd()} and {@link #getSpacingMultiplier()}. Default value is {@code zero}. |
| 1509 | * |
| 1510 | * @param line the index of the line, the value should be equal or greater than {@code zero} |
| 1511 | * @hide |
| 1512 | */ |
| 1513 | public int getLineExtra(@IntRange(from = 0) int line) { |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1517 | public int getOffsetToLeftOf(int offset) { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1518 | return getOffsetToLeftRightOf(offset, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | public int getOffsetToRightOf(int offset) { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1522 | return getOffsetToLeftRightOf(offset, false); |
| 1523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1525 | private int getOffsetToLeftRightOf(int caret, boolean toLeft) { |
| 1526 | int line = getLineForOffset(caret); |
| 1527 | int lineStart = getLineStart(line); |
| 1528 | int lineEnd = getLineEnd(line); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1529 | int lineDir = getParagraphDirection(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 1531 | boolean lineChanged = false; |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1532 | boolean advance = toLeft == (lineDir == DIR_RIGHT_TO_LEFT); |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 1533 | // if walking off line, look at the line we're headed to |
| 1534 | if (advance) { |
| 1535 | if (caret == lineEnd) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1536 | if (line < getLineCount() - 1) { |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 1537 | lineChanged = true; |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1538 | ++line; |
| 1539 | } else { |
| 1540 | return caret; // at very end, don't move |
| 1541 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1542 | } |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 1543 | } else { |
| 1544 | if (caret == lineStart) { |
| 1545 | if (line > 0) { |
| 1546 | lineChanged = true; |
| 1547 | --line; |
| 1548 | } else { |
| 1549 | return caret; // at very start, don't move |
| 1550 | } |
| 1551 | } |
| 1552 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1553 | |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 1554 | if (lineChanged) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1555 | lineStart = getLineStart(line); |
| 1556 | lineEnd = getLineEnd(line); |
| 1557 | int newDir = getParagraphDirection(line); |
| 1558 | if (newDir != lineDir) { |
| 1559 | // unusual case. we want to walk onto the line, but it runs |
| 1560 | // in a different direction than this one, so we fake movement |
| 1561 | // in the opposite direction. |
| 1562 | toLeft = !toLeft; |
| 1563 | lineDir = newDir; |
| 1564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1566 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1567 | Directions directions = getLineDirections(line); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1568 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1569 | TextLine tl = TextLine.obtain(); |
| 1570 | // XXX: we don't care about tabs |
| 1571 | tl.set(mPaint, mText, lineStart, lineEnd, lineDir, directions, false, null); |
| 1572 | caret = lineStart + tl.getOffsetToLeftRightOf(caret - lineStart, toLeft); |
| 1573 | tl = TextLine.recycle(tl); |
| 1574 | return caret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | private int getOffsetAtStartOf(int offset) { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1578 | // XXX this probably should skip local reorderings and |
| 1579 | // zero-width characters, look at callers |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | if (offset == 0) |
| 1581 | return 0; |
| 1582 | |
| 1583 | CharSequence text = mText; |
| 1584 | char c = text.charAt(offset); |
| 1585 | |
| 1586 | if (c >= '\uDC00' && c <= '\uDFFF') { |
| 1587 | char c1 = text.charAt(offset - 1); |
| 1588 | |
| 1589 | if (c1 >= '\uD800' && c1 <= '\uDBFF') |
| 1590 | offset -= 1; |
| 1591 | } |
| 1592 | |
| 1593 | if (mSpannedText) { |
| 1594 | ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset, |
| 1595 | ReplacementSpan.class); |
| 1596 | |
| 1597 | for (int i = 0; i < spans.length; i++) { |
| 1598 | int start = ((Spanned) text).getSpanStart(spans[i]); |
| 1599 | int end = ((Spanned) text).getSpanEnd(spans[i]); |
| 1600 | |
| 1601 | if (start < offset && end > offset) |
| 1602 | offset = start; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return offset; |
| 1607 | } |
| 1608 | |
| 1609 | /** |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1610 | * Determine whether we should clamp cursor position. Currently it's |
| 1611 | * only robust for left-aligned displays. |
| 1612 | * @hide |
| 1613 | */ |
| 1614 | public boolean shouldClampCursor(int line) { |
| 1615 | // Only clamp cursor position in left-aligned displays. |
| 1616 | switch (getParagraphAlignment(line)) { |
| 1617 | case ALIGN_LEFT: |
| 1618 | return true; |
| 1619 | case ALIGN_NORMAL: |
| 1620 | return getParagraphDirection(line) > 0; |
| 1621 | default: |
| 1622 | return false; |
| 1623 | } |
| 1624 | |
| 1625 | } |
| 1626 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | * Fills in the specified Path with a representation of a cursor |
| 1628 | * at the specified offset. This will often be a vertical line |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 1629 | * but can be multiple discontinuous lines in text with multiple |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | * directionalities. |
| 1631 | */ |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1632 | public void getCursorPath(final int point, final Path dest, final CharSequence editingBuffer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | dest.reset(); |
| 1634 | |
| 1635 | int line = getLineForOffset(point); |
| 1636 | int top = getLineTop(line); |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1637 | int bottom = getLineBottomWithoutSpacing(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1639 | boolean clamped = shouldClampCursor(line); |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1640 | float h1 = getPrimaryHorizontal(point, clamped) - 0.5f; |
| 1641 | float h2 = isLevelBoundary(point) ? getSecondaryHorizontal(point, clamped) - 0.5f : h1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1643 | int caps = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SHIFT_ON) | |
| 1644 | TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_SELECTING); |
| 1645 | int fn = TextKeyListener.getMetaState(editingBuffer, TextKeyListener.META_ALT_ON); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | int dist = 0; |
| 1647 | |
| 1648 | if (caps != 0 || fn != 0) { |
| 1649 | dist = (bottom - top) >> 2; |
| 1650 | |
| 1651 | if (fn != 0) |
| 1652 | top += dist; |
| 1653 | if (caps != 0) |
| 1654 | bottom -= dist; |
| 1655 | } |
| 1656 | |
| 1657 | if (h1 < 0.5f) |
| 1658 | h1 = 0.5f; |
| 1659 | if (h2 < 0.5f) |
| 1660 | h2 = 0.5f; |
| 1661 | |
Jozef BABJAK | 2fb503f | 2011-03-17 09:54:51 +0100 | [diff] [blame] | 1662 | if (Float.compare(h1, h2) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | dest.moveTo(h1, top); |
| 1664 | dest.lineTo(h1, bottom); |
| 1665 | } else { |
| 1666 | dest.moveTo(h1, top); |
| 1667 | dest.lineTo(h1, (top + bottom) >> 1); |
| 1668 | |
| 1669 | dest.moveTo(h2, (top + bottom) >> 1); |
| 1670 | dest.lineTo(h2, bottom); |
| 1671 | } |
| 1672 | |
| 1673 | if (caps == 2) { |
| 1674 | dest.moveTo(h2, bottom); |
| 1675 | dest.lineTo(h2 - dist, bottom + dist); |
| 1676 | dest.lineTo(h2, bottom); |
| 1677 | dest.lineTo(h2 + dist, bottom + dist); |
| 1678 | } else if (caps == 1) { |
| 1679 | dest.moveTo(h2, bottom); |
| 1680 | dest.lineTo(h2 - dist, bottom + dist); |
| 1681 | |
| 1682 | dest.moveTo(h2 - dist, bottom + dist - 0.5f); |
| 1683 | dest.lineTo(h2 + dist, bottom + dist - 0.5f); |
| 1684 | |
| 1685 | dest.moveTo(h2 + dist, bottom + dist); |
| 1686 | dest.lineTo(h2, bottom); |
| 1687 | } |
| 1688 | |
| 1689 | if (fn == 2) { |
| 1690 | dest.moveTo(h1, top); |
| 1691 | dest.lineTo(h1 - dist, top - dist); |
| 1692 | dest.lineTo(h1, top); |
| 1693 | dest.lineTo(h1 + dist, top - dist); |
| 1694 | } else if (fn == 1) { |
| 1695 | dest.moveTo(h1, top); |
| 1696 | dest.lineTo(h1 - dist, top - dist); |
| 1697 | |
| 1698 | dest.moveTo(h1 - dist, top - dist + 0.5f); |
| 1699 | dest.lineTo(h1 + dist, top - dist + 0.5f); |
| 1700 | |
| 1701 | dest.moveTo(h1 + dist, top - dist); |
| 1702 | dest.lineTo(h1, top); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | private void addSelection(int line, int start, int end, |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1707 | int top, int bottom, SelectionRectangleConsumer consumer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | int linestart = getLineStart(line); |
| 1709 | int lineend = getLineEnd(line); |
| 1710 | Directions dirs = getLineDirections(line); |
| 1711 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1712 | if (lineend > linestart && mText.charAt(lineend - 1) == '\n') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | lineend--; |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1714 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1715 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1716 | for (int i = 0; i < dirs.mDirections.length; i += 2) { |
| 1717 | int here = linestart + dirs.mDirections[i]; |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1718 | int there = here + (dirs.mDirections[i + 1] & RUN_LENGTH_MASK); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1719 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1720 | if (there > lineend) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1721 | there = lineend; |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1722 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | |
| 1724 | if (start <= there && end >= here) { |
| 1725 | int st = Math.max(start, here); |
| 1726 | int en = Math.min(end, there); |
| 1727 | |
| 1728 | if (st != en) { |
Raph Levien | afe8e9b | 2012-12-19 16:09:32 -0800 | [diff] [blame] | 1729 | float h1 = getHorizontal(st, false, line, false /* not clamped */); |
| 1730 | float h2 = getHorizontal(en, true, line, false /* not clamped */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | |
Fabrice Di Meglio | 3716601 | 2011-08-31 13:56:37 -0700 | [diff] [blame] | 1732 | float left = Math.min(h1, h2); |
| 1733 | float right = Math.max(h1, h2); |
| 1734 | |
Petar Šegina | 8f1f74e | 2017-09-07 14:26:28 +0100 | [diff] [blame^] | 1735 | final @TextSelectionLayout int layout = |
| 1736 | ((dirs.mDirections[i + 1] & RUN_RTL_FLAG) != 0) |
| 1737 | ? TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT |
| 1738 | : TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT; |
| 1739 | |
| 1740 | consumer.accept(left, top, right, bottom, layout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | } |
| 1742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * Fills in the specified Path with a representation of a highlight |
| 1748 | * between the specified offsets. This will often be a rectangle |
| 1749 | * or a potentially discontinuous set of rectangles. If the start |
| 1750 | * and end are the same, the returned path is empty. |
| 1751 | */ |
| 1752 | public void getSelectionPath(int start, int end, Path dest) { |
| 1753 | dest.reset(); |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1754 | getSelection(start, end, (left, top, right, bottom, textSelectionLayout) -> |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1755 | dest.addRect(left, top, right, bottom, Path.Direction.CW)); |
| 1756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1758 | /** |
| 1759 | * Calculates the rectangles which should be highlighted to indicate a selection between start |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1760 | * and end and feeds them into the given {@link SelectionRectangleConsumer}. |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1761 | * |
| 1762 | * @param start the starting index of the selection |
| 1763 | * @param end the ending index of the selection |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1764 | * @param consumer the {@link SelectionRectangleConsumer} which will receive the generated |
| 1765 | * rectangles. It will be called every time a rectangle is generated. |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1766 | * @hide |
| 1767 | * @see #getSelectionPath(int, int, Path) |
| 1768 | */ |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1769 | public final void getSelection(int start, int end, final SelectionRectangleConsumer consumer) { |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1770 | if (start == end) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | return; |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | |
| 1774 | if (end < start) { |
| 1775 | int temp = end; |
| 1776 | end = start; |
| 1777 | start = temp; |
| 1778 | } |
| 1779 | |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1780 | final int startline = getLineForOffset(start); |
| 1781 | final int endline = getLineForOffset(end); |
Roozbeh Pournader | 7557a5a | 2017-04-11 18:34:42 -0700 | [diff] [blame] | 1782 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | int top = getLineTop(startline); |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1784 | int bottom = getLineBottomWithoutSpacing(endline); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1785 | |
| 1786 | if (startline == endline) { |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1787 | addSelection(startline, start, end, top, bottom, consumer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | } else { |
| 1789 | final float width = mWidth; |
| 1790 | |
| 1791 | addSelection(startline, start, getLineEnd(startline), |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1792 | top, getLineBottom(startline), consumer); |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 1793 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1794 | if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) { |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1795 | consumer.accept(getLineLeft(startline), top, 0, getLineBottom(startline), |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1796 | TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT); |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1797 | } else { |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1798 | consumer.accept(getLineRight(startline), top, width, getLineBottom(startline), |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1799 | TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT); |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | |
| 1802 | for (int i = startline + 1; i < endline; i++) { |
| 1803 | top = getLineTop(i); |
| 1804 | bottom = getLineBottom(i); |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1805 | if (getParagraphDirection(i) == DIR_RIGHT_TO_LEFT) { |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1806 | consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT); |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1807 | } else { |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1808 | consumer.accept(0, top, width, bottom, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT); |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | top = getLineTop(endline); |
Siyamed Sinir | a60b59d | 2017-07-26 09:26:41 -0700 | [diff] [blame] | 1813 | bottom = getLineBottomWithoutSpacing(endline); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1815 | addSelection(endline, getLineStart(endline), end, top, bottom, consumer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1816 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1817 | if (getParagraphDirection(endline) == DIR_RIGHT_TO_LEFT) { |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1818 | consumer.accept(width, top, getLineRight(endline), bottom, |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1819 | TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT); |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1820 | } else { |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 1821 | consumer.accept(0, top, getLineLeft(endline), bottom, |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 1822 | TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT); |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 1823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /** |
| 1828 | * Get the alignment of the specified paragraph, taking into account |
| 1829 | * markup attached to it. |
| 1830 | */ |
| 1831 | public final Alignment getParagraphAlignment(int line) { |
| 1832 | Alignment align = mAlignment; |
| 1833 | |
| 1834 | if (mSpannedText) { |
| 1835 | Spanned sp = (Spanned) mText; |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 1836 | AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | getLineEnd(line), |
| 1838 | AlignmentSpan.class); |
| 1839 | |
| 1840 | int spanLength = spans.length; |
| 1841 | if (spanLength > 0) { |
| 1842 | align = spans[spanLength-1].getAlignment(); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | return align; |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * Get the left edge of the specified paragraph, inset by left margins. |
| 1851 | */ |
| 1852 | public final int getParagraphLeft(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | int left = 0; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1854 | int dir = getParagraphDirection(line); |
| 1855 | if (dir == DIR_RIGHT_TO_LEFT || !mSpannedText) { |
| 1856 | return left; // leading margin has no impact, or no styles |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1858 | return getParagraphLeadingMargin(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /** |
| 1862 | * Get the right edge of the specified paragraph, inset by right margins. |
| 1863 | */ |
| 1864 | public final int getParagraphRight(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | int right = mWidth; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1866 | int dir = getParagraphDirection(line); |
| 1867 | if (dir == DIR_LEFT_TO_RIGHT || !mSpannedText) { |
| 1868 | return right; // leading margin has no impact, or no styles |
| 1869 | } |
| 1870 | return right - getParagraphLeadingMargin(line); |
| 1871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1873 | /** |
| 1874 | * Returns the effective leading margin (unsigned) for this line, |
| 1875 | * taking into account LeadingMarginSpan and LeadingMarginSpan2. |
| 1876 | * @param line the line index |
| 1877 | * @return the leading margin of this line |
| 1878 | */ |
| 1879 | private int getParagraphLeadingMargin(int line) { |
| 1880 | if (!mSpannedText) { |
| 1881 | return 0; |
| 1882 | } |
| 1883 | Spanned spanned = (Spanned) mText; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1884 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1885 | int lineStart = getLineStart(line); |
| 1886 | int lineEnd = getLineEnd(line); |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1887 | int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd, |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1888 | LeadingMarginSpan.class); |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 1889 | LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd, |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1890 | LeadingMarginSpan.class); |
| 1891 | if (spans.length == 0) { |
| 1892 | return 0; // no leading margin span; |
| 1893 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1894 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1895 | int margin = 0; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1896 | |
| 1897 | boolean isFirstParaLine = lineStart == 0 || |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1898 | spanned.charAt(lineStart - 1) == '\n'; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1899 | |
Anish Athalye | ab08c6d | 2014-08-08 12:09:58 -0700 | [diff] [blame] | 1900 | boolean useFirstLineMargin = isFirstParaLine; |
| 1901 | for (int i = 0; i < spans.length; i++) { |
| 1902 | if (spans[i] instanceof LeadingMarginSpan2) { |
| 1903 | int spStart = spanned.getSpanStart(spans[i]); |
| 1904 | int spanLine = getLineForOffset(spStart); |
| 1905 | int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount(); |
| 1906 | // if there is more than one LeadingMarginSpan2, use the count that is greatest |
| 1907 | useFirstLineMargin |= line < spanLine + count; |
| 1908 | } |
| 1909 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1910 | for (int i = 0; i < spans.length; i++) { |
| 1911 | LeadingMarginSpan span = spans[i]; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1912 | margin += span.getLeadingMargin(useFirstLineMargin); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1915 | return margin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1918 | /* package */ |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 1919 | static float measurePara(TextPaint paint, CharSequence text, int start, int end, |
| 1920 | TextDirectionHeuristic textDir) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1921 | MeasuredText mt = MeasuredText.obtain(); |
| 1922 | TextLine tl = TextLine.obtain(); |
| 1923 | try { |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 1924 | mt.setPara(text, start, end, textDir, null); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1925 | Directions directions; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1926 | int dir; |
| 1927 | if (mt.mEasy) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1928 | directions = DIRS_ALL_LEFT_TO_RIGHT; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1929 | dir = Layout.DIR_LEFT_TO_RIGHT; |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1930 | } else { |
| 1931 | directions = AndroidBidi.directions(mt.mDir, mt.mLevels, |
| 1932 | 0, mt.mChars, 0, mt.mLen); |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1933 | dir = mt.mDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1934 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1935 | char[] chars = mt.mChars; |
| 1936 | int len = mt.mLen; |
| 1937 | boolean hasTabs = false; |
| 1938 | TabStops tabStops = null; |
Anish Athalye | c14b3ad | 2014-07-24 18:03:21 -0700 | [diff] [blame] | 1939 | // leading margins should be taken into account when measuring a paragraph |
| 1940 | int margin = 0; |
| 1941 | if (text instanceof Spanned) { |
| 1942 | Spanned spanned = (Spanned) text; |
| 1943 | LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end, |
| 1944 | LeadingMarginSpan.class); |
| 1945 | for (LeadingMarginSpan lms : spans) { |
| 1946 | margin += lms.getLeadingMargin(true); |
| 1947 | } |
| 1948 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1949 | for (int i = 0; i < len; ++i) { |
| 1950 | if (chars[i] == '\t') { |
| 1951 | hasTabs = true; |
| 1952 | if (text instanceof Spanned) { |
| 1953 | Spanned spanned = (Spanned) text; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1954 | int spanEnd = spanned.nextSpanTransition(start, end, |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1955 | TabStopSpan.class); |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 1956 | TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd, |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1957 | TabStopSpan.class); |
| 1958 | if (spans.length > 0) { |
| 1959 | tabStops = new TabStops(TAB_INCREMENT, spans); |
| 1960 | } |
| 1961 | } |
| 1962 | break; |
| 1963 | } |
| 1964 | } |
| 1965 | tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops); |
Siyamed Sinir | 79bf9d1 | 2016-05-18 19:57:52 -0700 | [diff] [blame] | 1966 | return margin + Math.abs(tl.metrics(null)); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1967 | } finally { |
| 1968 | TextLine.recycle(tl); |
| 1969 | MeasuredText.recycle(mt); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1970 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | } |
| 1972 | |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 1973 | /** |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1974 | * @hide |
| 1975 | */ |
| 1976 | /* package */ static class TabStops { |
| 1977 | private int[] mStops; |
| 1978 | private int mNumStops; |
| 1979 | private int mIncrement; |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1980 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1981 | TabStops(int increment, Object[] spans) { |
| 1982 | reset(increment, spans); |
| 1983 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 1984 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1985 | void reset(int increment, Object[] spans) { |
| 1986 | this.mIncrement = increment; |
| 1987 | |
| 1988 | int ns = 0; |
| 1989 | if (spans != null) { |
| 1990 | int[] stops = this.mStops; |
| 1991 | for (Object o : spans) { |
| 1992 | if (o instanceof TabStopSpan) { |
| 1993 | if (stops == null) { |
| 1994 | stops = new int[10]; |
| 1995 | } else if (ns == stops.length) { |
| 1996 | int[] nstops = new int[ns * 2]; |
| 1997 | for (int i = 0; i < ns; ++i) { |
| 1998 | nstops[i] = stops[i]; |
| 1999 | } |
| 2000 | stops = nstops; |
| 2001 | } |
| 2002 | stops[ns++] = ((TabStopSpan) o).getTabStop(); |
| 2003 | } |
| 2004 | } |
| 2005 | if (ns > 1) { |
| 2006 | Arrays.sort(stops, 0, ns); |
| 2007 | } |
| 2008 | if (stops != this.mStops) { |
| 2009 | this.mStops = stops; |
| 2010 | } |
| 2011 | } |
| 2012 | this.mNumStops = ns; |
| 2013 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 2014 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 2015 | float nextTab(float h) { |
| 2016 | int ns = this.mNumStops; |
| 2017 | if (ns > 0) { |
| 2018 | int[] stops = this.mStops; |
| 2019 | for (int i = 0; i < ns; ++i) { |
| 2020 | int stop = stops[i]; |
| 2021 | if (stop > h) { |
| 2022 | return stop; |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | return nextDefaultStop(h, mIncrement); |
| 2027 | } |
| 2028 | |
| 2029 | public static float nextDefaultStop(float h, int inc) { |
| 2030 | return ((int) ((h + inc) / inc)) * inc; |
| 2031 | } |
| 2032 | } |
Doug Felt | 0c702b8 | 2010-05-14 10:55:42 -0700 | [diff] [blame] | 2033 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 2034 | /** |
Doug Felt | 71b8dd7 | 2010-02-16 17:27:09 -0800 | [diff] [blame] | 2035 | * Returns the position of the next tab stop after h on the line. |
| 2036 | * |
| 2037 | * @param text the text |
| 2038 | * @param start start of the line |
| 2039 | * @param end limit of the line |
| 2040 | * @param h the current horizontal offset |
| 2041 | * @param tabs the tabs, can be null. If it is null, any tabs in effect |
| 2042 | * on the line will be used. If there are no tabs, a default offset |
| 2043 | * will be used to compute the tab stop. |
| 2044 | * @return the offset of the next tab stop. |
| 2045 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2046 | /* package */ static float nextTab(CharSequence text, int start, int end, |
| 2047 | float h, Object[] tabs) { |
| 2048 | float nh = Float.MAX_VALUE; |
| 2049 | boolean alltabs = false; |
| 2050 | |
| 2051 | if (text instanceof Spanned) { |
| 2052 | if (tabs == null) { |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 2053 | tabs = getParagraphSpans((Spanned) text, start, end, TabStopSpan.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2054 | alltabs = true; |
| 2055 | } |
| 2056 | |
| 2057 | for (int i = 0; i < tabs.length; i++) { |
| 2058 | if (!alltabs) { |
| 2059 | if (!(tabs[i] instanceof TabStopSpan)) |
| 2060 | continue; |
| 2061 | } |
| 2062 | |
| 2063 | int where = ((TabStopSpan) tabs[i]).getTabStop(); |
| 2064 | |
| 2065 | if (where < nh && where > h) |
| 2066 | nh = where; |
| 2067 | } |
| 2068 | |
| 2069 | if (nh != Float.MAX_VALUE) |
| 2070 | return nh; |
| 2071 | } |
| 2072 | |
| 2073 | return ((int) ((h + TAB_INCREMENT) / TAB_INCREMENT)) * TAB_INCREMENT; |
| 2074 | } |
| 2075 | |
| 2076 | protected final boolean isSpanned() { |
| 2077 | return mSpannedText; |
| 2078 | } |
| 2079 | |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 2080 | /** |
| 2081 | * Returns the same as <code>text.getSpans()</code>, except where |
| 2082 | * <code>start</code> and <code>end</code> are the same and are not |
| 2083 | * at the very beginning of the text, in which case an empty array |
| 2084 | * is returned instead. |
| 2085 | * <p> |
| 2086 | * This is needed because of the special case that <code>getSpans()</code> |
| 2087 | * on an empty range returns the spans adjacent to that range, which is |
| 2088 | * primarily for the sake of <code>TextWatchers</code> so they will get |
| 2089 | * notifications when text goes from empty to non-empty. But it also |
| 2090 | * has the unfortunate side effect that if the text ends with an empty |
| 2091 | * paragraph, that paragraph accidentally picks up the styles of the |
| 2092 | * preceding paragraph (even though those styles will not be picked up |
| 2093 | * by new text that is inserted into the empty paragraph). |
| 2094 | * <p> |
| 2095 | * The reason it just checks whether <code>start</code> and <code>end</code> |
| 2096 | * is the same is that the only time a line can contain 0 characters |
| 2097 | * is if it is the final paragraph of the Layout; otherwise any line will |
| 2098 | * contain at least one printing or newline character. The reason for the |
| 2099 | * additional check if <code>start</code> is greater than 0 is that |
| 2100 | * if the empty paragraph is the entire content of the buffer, paragraph |
| 2101 | * styles that are already applied to the buffer will apply to text that |
| 2102 | * is inserted into it. |
| 2103 | */ |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 2104 | /* package */static <T> T[] getParagraphSpans(Spanned text, int start, int end, Class<T> type) { |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 2105 | if (start == end && start > 0) { |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 2106 | return ArrayUtils.emptyArray(type); |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Siyamed Sinir | fa05ba0 | 2016-01-12 10:54:43 -0800 | [diff] [blame] | 2109 | if(text instanceof SpannableStringBuilder) { |
| 2110 | return ((SpannableStringBuilder) text).getSpans(start, end, type, false); |
| 2111 | } else { |
| 2112 | return text.getSpans(start, end, type); |
| 2113 | } |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | private void ellipsize(int start, int end, int line, |
Fabrice Di Meglio | 8d44fff | 2012-06-13 15:45:38 -0700 | [diff] [blame] | 2117 | char[] dest, int destoff, TextUtils.TruncateAt method) { |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2118 | final int ellipsisCount = getEllipsisCount(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2119 | if (ellipsisCount == 0) { |
| 2120 | return; |
| 2121 | } |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2122 | final int ellipsisStart = getEllipsisStart(line); |
| 2123 | final int lineStart = getLineStart(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2124 | |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2125 | final String ellipsisString = TextUtils.getEllipsisString(method); |
| 2126 | final int ellipsisStringLen = ellipsisString.length(); |
Roozbeh Pournader | 1051bbe | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 2127 | // Use the ellipsis string only if there are that at least as many characters to replace. |
| 2128 | final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen; |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2129 | for (int i = 0; i < ellipsisCount; i++) { |
| 2130 | final char c; |
Roozbeh Pournader | 1051bbe | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 2131 | if (useEllipsisString && i < ellipsisStringLen) { |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2132 | c = ellipsisString.charAt(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | } else { |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2134 | c = TextUtils.ELLIPSIS_FILLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Roozbeh Pournader | 9ea756f | 2017-07-25 11:20:29 -0700 | [diff] [blame] | 2137 | final int a = i + ellipsisStart + lineStart; |
| 2138 | if (start <= a && a < end) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | dest[destoff + a - start] = c; |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * Stores information about bidirectional (left-to-right or right-to-left) |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2146 | * text within the layout of a line. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2147 | */ |
| 2148 | public static class Directions { |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2149 | // Directions represents directional runs within a line of text. |
| 2150 | // Runs are pairs of ints listed in visual order, starting from the |
| 2151 | // leading margin. The first int of each pair is the offset from |
| 2152 | // the first character of the line to the start of the run. The |
| 2153 | // second int represents both the length and level of the run. |
| 2154 | // The length is in the lower bits, accessed by masking with |
| 2155 | // DIR_LENGTH_MASK. The level is in the higher bits, accessed |
| 2156 | // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK. |
| 2157 | // To simply test for an RTL direction, test the bit using |
| 2158 | // DIR_RTL_FLAG, if set then the direction is rtl. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | |
Siyamed Sinir | ed09ae1 | 2016-02-16 14:36:26 -0800 | [diff] [blame] | 2160 | /** |
| 2161 | * @hide |
| 2162 | */ |
| 2163 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 2164 | public int[] mDirections; |
| 2165 | |
| 2166 | /** |
| 2167 | * @hide |
| 2168 | */ |
| 2169 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 2170 | public Directions(int[] dirs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2171 | mDirections = dirs; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * Return the offset of the first character to be ellipsized away, |
| 2177 | * relative to the start of the line. (So 0 if the beginning of the |
| 2178 | * line is ellipsized, not getLineStart().) |
| 2179 | */ |
| 2180 | public abstract int getEllipsisStart(int line); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 2181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | /** |
| 2183 | * Returns the number of characters to be ellipsized away, or 0 if |
| 2184 | * no ellipsis is to take place. |
| 2185 | */ |
| 2186 | public abstract int getEllipsisCount(int line); |
| 2187 | |
| 2188 | /* package */ static class Ellipsizer implements CharSequence, GetChars { |
| 2189 | /* package */ CharSequence mText; |
| 2190 | /* package */ Layout mLayout; |
| 2191 | /* package */ int mWidth; |
| 2192 | /* package */ TextUtils.TruncateAt mMethod; |
| 2193 | |
| 2194 | public Ellipsizer(CharSequence s) { |
| 2195 | mText = s; |
| 2196 | } |
| 2197 | |
| 2198 | public char charAt(int off) { |
| 2199 | char[] buf = TextUtils.obtain(1); |
| 2200 | getChars(off, off + 1, buf, 0); |
| 2201 | char ret = buf[0]; |
| 2202 | |
| 2203 | TextUtils.recycle(buf); |
| 2204 | return ret; |
| 2205 | } |
| 2206 | |
| 2207 | public void getChars(int start, int end, char[] dest, int destoff) { |
| 2208 | int line1 = mLayout.getLineForOffset(start); |
| 2209 | int line2 = mLayout.getLineForOffset(end); |
| 2210 | |
| 2211 | TextUtils.getChars(mText, start, end, dest, destoff); |
| 2212 | |
| 2213 | for (int i = line1; i <= line2; i++) { |
Fabrice Di Meglio | 8d44fff | 2012-06-13 15:45:38 -0700 | [diff] [blame] | 2214 | mLayout.ellipsize(start, end, i, dest, destoff, mMethod); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | public int length() { |
| 2219 | return mText.length(); |
| 2220 | } |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2222 | public CharSequence subSequence(int start, int end) { |
| 2223 | char[] s = new char[end - start]; |
| 2224 | getChars(start, end, s, 0); |
| 2225 | return new String(s); |
| 2226 | } |
| 2227 | |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 2228 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | public String toString() { |
| 2230 | char[] s = new char[length()]; |
| 2231 | getChars(0, length(), s, 0); |
| 2232 | return new String(s); |
| 2233 | } |
| 2234 | |
| 2235 | } |
| 2236 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 2237 | /* package */ static class SpannedEllipsizer extends Ellipsizer implements Spanned { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2238 | private Spanned mSpanned; |
| 2239 | |
| 2240 | public SpannedEllipsizer(CharSequence display) { |
| 2241 | super(display); |
| 2242 | mSpanned = (Spanned) display; |
| 2243 | } |
| 2244 | |
| 2245 | public <T> T[] getSpans(int start, int end, Class<T> type) { |
| 2246 | return mSpanned.getSpans(start, end, type); |
| 2247 | } |
| 2248 | |
| 2249 | public int getSpanStart(Object tag) { |
| 2250 | return mSpanned.getSpanStart(tag); |
| 2251 | } |
| 2252 | |
| 2253 | public int getSpanEnd(Object tag) { |
| 2254 | return mSpanned.getSpanEnd(tag); |
| 2255 | } |
| 2256 | |
| 2257 | public int getSpanFlags(Object tag) { |
| 2258 | return mSpanned.getSpanFlags(tag); |
| 2259 | } |
| 2260 | |
Gilles Debunne | 6c488de | 2012-03-01 16:20:35 -0800 | [diff] [blame] | 2261 | @SuppressWarnings("rawtypes") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | public int nextSpanTransition(int start, int limit, Class type) { |
| 2263 | return mSpanned.nextSpanTransition(start, limit, type); |
| 2264 | } |
| 2265 | |
Gilles Debunne | 162bf0f | 2010-11-16 16:23:53 -0800 | [diff] [blame] | 2266 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2267 | public CharSequence subSequence(int start, int end) { |
| 2268 | char[] s = new char[end - start]; |
| 2269 | getChars(start, end, s, 0); |
| 2270 | |
| 2271 | SpannableString ss = new SpannableString(new String(s)); |
| 2272 | TextUtils.copySpansFrom(mSpanned, start, end, Object.class, ss, 0); |
| 2273 | return ss; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | private CharSequence mText; |
| 2278 | private TextPaint mPaint; |
Roozbeh Pournader | b1f0365 | 2017-08-08 15:49:01 -0700 | [diff] [blame] | 2279 | private TextPaint mWorkPaint = new TextPaint(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | private int mWidth; |
| 2281 | private Alignment mAlignment = Alignment.ALIGN_NORMAL; |
| 2282 | private float mSpacingMult; |
| 2283 | private float mSpacingAdd; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 2284 | private static final Rect sTempRect = new Rect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | private boolean mSpannedText; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2286 | private TextDirectionHeuristic mTextDir; |
Gilles Debunne | eca5b73 | 2012-04-25 18:48:42 -0700 | [diff] [blame] | 2287 | private SpanSet<LineBackgroundSpan> mLineBackgroundSpans; |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 2288 | private int mJustificationMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | |
| 2290 | public static final int DIR_LEFT_TO_RIGHT = 1; |
| 2291 | public static final int DIR_RIGHT_TO_LEFT = -1; |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2292 | |
Doug Felt | 20178d6 | 2010-02-22 13:39:01 -0800 | [diff] [blame] | 2293 | /* package */ static final int DIR_REQUEST_LTR = 1; |
| 2294 | /* package */ static final int DIR_REQUEST_RTL = -1; |
| 2295 | /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2; |
| 2296 | /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2297 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2298 | /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff; |
| 2299 | /* package */ static final int RUN_LEVEL_SHIFT = 26; |
| 2300 | /* package */ static final int RUN_LEVEL_MASK = 0x3f; |
| 2301 | /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT; |
| 2302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2303 | public enum Alignment { |
| 2304 | ALIGN_NORMAL, |
| 2305 | ALIGN_OPPOSITE, |
| 2306 | ALIGN_CENTER, |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 2307 | /** @hide */ |
| 2308 | ALIGN_LEFT, |
| 2309 | /** @hide */ |
| 2310 | ALIGN_RIGHT, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | private static final int TAB_INCREMENT = 20; |
| 2314 | |
Siyamed Sinir | ed09ae1 | 2016-02-16 14:36:26 -0800 | [diff] [blame] | 2315 | /** @hide */ |
| 2316 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 2317 | public static final Directions DIRS_ALL_LEFT_TO_RIGHT = |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2318 | new Directions(new int[] { 0, RUN_LENGTH_MASK }); |
Siyamed Sinir | ed09ae1 | 2016-02-16 14:36:26 -0800 | [diff] [blame] | 2319 | |
| 2320 | /** @hide */ |
| 2321 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 2322 | public static final Directions DIRS_ALL_RIGHT_TO_LEFT = |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 2323 | new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG }); |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 2324 | |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 2325 | /** @hide */ |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 2326 | @Retention(RetentionPolicy.SOURCE) |
Petar Šegina | 3a92fb6 | 2017-09-07 21:03:24 +0100 | [diff] [blame] | 2327 | @IntDef({TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT, TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT}) |
| 2328 | public @interface TextSelectionLayout {} |
| 2329 | |
| 2330 | /** @hide */ |
| 2331 | public static final int TEXT_SELECTION_LAYOUT_RIGHT_TO_LEFT = 0; |
| 2332 | /** @hide */ |
| 2333 | public static final int TEXT_SELECTION_LAYOUT_LEFT_TO_RIGHT = 1; |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 2334 | |
| 2335 | /** @hide */ |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 2336 | @FunctionalInterface |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 2337 | public interface SelectionRectangleConsumer { |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 2338 | /** |
| 2339 | * Performs this operation on the given rectangle. |
| 2340 | * |
| 2341 | * @param left the left edge of the rectangle |
| 2342 | * @param top the top edge of the rectangle |
| 2343 | * @param right the right edge of the rectangle |
| 2344 | * @param bottom the bottom edge of the rectangle |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 2345 | * @param textSelectionLayout the layout (RTL or LTR) of the text covered by this |
| 2346 | * selection rectangle |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 2347 | */ |
Petar Šegina | b92c539 | 2017-09-06 15:25:05 +0100 | [diff] [blame] | 2348 | void accept(float left, float top, float right, float bottom, |
| 2349 | @TextSelectionLayout int textSelectionLayout); |
Petar Šegina | fb748b3 | 2017-08-07 12:37:52 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2352 | } |