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