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