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