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