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