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