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