The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 19 | import android.annotation.FloatRange; |
| 20 | import android.annotation.IntRange; |
| 21 | import android.annotation.NonNull; |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 23 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.graphics.Paint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.text.style.LeadingMarginSpan; |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 26 | import android.text.style.LeadingMarginSpan.LeadingMarginSpan2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.text.style.LineHeightSpan; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 28 | import android.text.style.TabStopSpan; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 30 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 32 | import com.android.internal.util.ArrayUtils; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 33 | import com.android.internal.util.GrowingArrayUtils; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 34 | |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 35 | import java.util.Arrays; |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | /** |
| 38 | * StaticLayout is a Layout for text that will not be edited after it |
| 39 | * is laid out. Use {@link DynamicLayout} for text that may change. |
| 40 | * <p>This is used by widgets to control text layout. You should not need |
| 41 | * to use this class directly unless you are implementing your own widget |
| 42 | * or custom display object, or would be tempted to call |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 43 | * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, |
| 44 | * float, float, android.graphics.Paint) |
| 45 | * Canvas.drawText()} directly.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | */ |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 47 | public class StaticLayout extends Layout { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 48 | /* |
| 49 | * The break iteration is done in native code. The protocol for using the native code is as |
| 50 | * follows. |
| 51 | * |
| 52 | * First, call nInit to setup native line breaker object. Then, for each paragraph, do the |
| 53 | * following: |
| 54 | * |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 55 | * - Create MeasuredParagraph by MeasuredParagraph.buildForStaticLayout which measures in |
| 56 | * native. |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 57 | * - Run NativeLineBreaker.computeLineBreaks() to obtain line breaks for the paragraph. |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 58 | * |
| 59 | * After all paragraphs, call finish() to release expensive buffers. |
| 60 | */ |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 61 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 62 | static final String TAG = "StaticLayout"; |
| 63 | |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 64 | /** |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 65 | * Builder for static layouts. The builder is the preferred pattern for constructing |
| 66 | * StaticLayout objects and should be preferred over the constructors, particularly to access |
| 67 | * newer features. To build a static layout, first call {@link #obtain} with the required |
| 68 | * arguments (text, paint, and width), then call setters for optional parameters, and finally |
| 69 | * {@link #build} to build the StaticLayout object. Parameters not explicitly set will get |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 70 | * default values. |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 71 | */ |
| 72 | public final static class Builder { |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 73 | private Builder() {} |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 74 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 75 | /** |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 76 | * Obtain a builder for constructing StaticLayout objects. |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 77 | * |
| 78 | * @param source The text to be laid out, optionally with spans |
| 79 | * @param start The index of the start of the text |
| 80 | * @param end The index + 1 of the end of the text |
| 81 | * @param paint The base paint used for layout |
| 82 | * @param width The width in pixels |
| 83 | * @return a builder object used for constructing the StaticLayout |
| 84 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 85 | @NonNull |
| 86 | public static Builder obtain(@NonNull CharSequence source, @IntRange(from = 0) int start, |
| 87 | @IntRange(from = 0) int end, @NonNull TextPaint paint, |
| 88 | @IntRange(from = 0) int width) { |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 89 | Builder b = sPool.acquire(); |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 90 | if (b == null) { |
| 91 | b = new Builder(); |
| 92 | } |
| 93 | |
| 94 | // set default initial values |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 95 | b.mText = source; |
| 96 | b.mStart = start; |
| 97 | b.mEnd = end; |
Raph Levien | ebd66ca | 2015-04-30 15:27:57 -0700 | [diff] [blame] | 98 | b.mPaint = paint; |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 99 | b.mWidth = width; |
| 100 | b.mAlignment = Alignment.ALIGN_NORMAL; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 101 | b.mTextDir = TextDirectionHeuristics.FIRSTSTRONG_LTR; |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 102 | b.mSpacingMult = DEFAULT_LINESPACING_MULTIPLIER; |
| 103 | b.mSpacingAdd = DEFAULT_LINESPACING_ADDITION; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 104 | b.mIncludePad = true; |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 105 | b.mFallbackLineSpacing = false; |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 106 | b.mEllipsizedWidth = width; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 107 | b.mEllipsize = null; |
| 108 | b.mMaxLines = Integer.MAX_VALUE; |
Raph Levien | 3bd60c7 | 2015-05-06 14:26:35 -0700 | [diff] [blame] | 109 | b.mBreakStrategy = Layout.BREAK_STRATEGY_SIMPLE; |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 110 | b.mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE; |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 111 | b.mJustificationMode = Layout.JUSTIFICATION_MODE_NONE; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 112 | return b; |
| 113 | } |
| 114 | |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 115 | /** |
| 116 | * This method should be called after the layout is finished getting constructed and the |
| 117 | * builder needs to be cleaned up and returned to the pool. |
| 118 | */ |
| 119 | private static void recycle(@NonNull Builder b) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 120 | b.mPaint = null; |
| 121 | b.mText = null; |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 122 | b.mLeftIndents = null; |
| 123 | b.mRightIndents = null; |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 124 | sPool.release(b); |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // release any expensive state |
| 128 | /* package */ void finish() { |
Raph Levien | 22ba786 | 2015-07-29 12:34:13 -0700 | [diff] [blame] | 129 | mText = null; |
| 130 | mPaint = null; |
| 131 | mLeftIndents = null; |
| 132 | mRightIndents = null; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public Builder setText(CharSequence source) { |
| 136 | return setText(source, 0, source.length()); |
| 137 | } |
| 138 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Set the text. Only useful when re-using the builder, which is done for |
| 141 | * the internal implementation of {@link DynamicLayout} but not as part |
| 142 | * of normal {@link StaticLayout} usage. |
| 143 | * |
| 144 | * @param source The text to be laid out, optionally with spans |
| 145 | * @param start The index of the start of the text |
| 146 | * @param end The index + 1 of the end of the text |
| 147 | * @return this builder, useful for chaining |
| 148 | * |
| 149 | * @hide |
| 150 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 151 | @NonNull |
| 152 | public Builder setText(@NonNull CharSequence source, int start, int end) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 153 | mText = source; |
| 154 | mStart = start; |
| 155 | mEnd = end; |
| 156 | return this; |
| 157 | } |
| 158 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Set the paint. Internal for reuse cases only. |
| 161 | * |
| 162 | * @param paint The base paint used for layout |
| 163 | * @return this builder, useful for chaining |
| 164 | * |
| 165 | * @hide |
| 166 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 167 | @NonNull |
| 168 | public Builder setPaint(@NonNull TextPaint paint) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 169 | mPaint = paint; |
| 170 | return this; |
| 171 | } |
| 172 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Set the width. Internal for reuse cases only. |
| 175 | * |
| 176 | * @param width The width in pixels |
| 177 | * @return this builder, useful for chaining |
| 178 | * |
| 179 | * @hide |
| 180 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 181 | @NonNull |
| 182 | public Builder setWidth(@IntRange(from = 0) int width) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 183 | mWidth = width; |
| 184 | if (mEllipsize == null) { |
| 185 | mEllipsizedWidth = width; |
| 186 | } |
| 187 | return this; |
| 188 | } |
| 189 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Set the alignment. The default is {@link Layout.Alignment#ALIGN_NORMAL}. |
| 192 | * |
| 193 | * @param alignment Alignment for the resulting {@link StaticLayout} |
| 194 | * @return this builder, useful for chaining |
| 195 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 196 | @NonNull |
| 197 | public Builder setAlignment(@NonNull Alignment alignment) { |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 198 | mAlignment = alignment; |
| 199 | return this; |
| 200 | } |
| 201 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Set the text direction heuristic. The text direction heuristic is used to |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 204 | * resolve text direction per-paragraph based on the input text. The default is |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 205 | * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR}. |
| 206 | * |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 207 | * @param textDir text direction heuristic for resolving bidi behavior. |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 208 | * @return this builder, useful for chaining |
| 209 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 210 | @NonNull |
| 211 | public Builder setTextDirection(@NonNull TextDirectionHeuristic textDir) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 212 | mTextDir = textDir; |
| 213 | return this; |
| 214 | } |
| 215 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 216 | /** |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 217 | * Set line spacing parameters. Each line will have its line spacing multiplied by |
| 218 | * {@code spacingMult} and then increased by {@code spacingAdd}. The default is 0.0 for |
| 219 | * {@code spacingAdd} and 1.0 for {@code spacingMult}. |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 220 | * |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 221 | * @param spacingAdd the amount of line spacing addition |
| 222 | * @param spacingMult the line spacing multiplier |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 223 | * @return this builder, useful for chaining |
| 224 | * @see android.widget.TextView#setLineSpacing |
| 225 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 226 | @NonNull |
| 227 | public Builder setLineSpacing(float spacingAdd, @FloatRange(from = 0.0) float spacingMult) { |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 228 | mSpacingAdd = spacingAdd; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 229 | mSpacingMult = spacingMult; |
| 230 | return this; |
| 231 | } |
| 232 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Set whether to include extra space beyond font ascent and descent (which is |
| 235 | * needed to avoid clipping in some languages, such as Arabic and Kannada). The |
| 236 | * default is {@code true}. |
| 237 | * |
| 238 | * @param includePad whether to include padding |
| 239 | * @return this builder, useful for chaining |
| 240 | * @see android.widget.TextView#setIncludeFontPadding |
| 241 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 242 | @NonNull |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 243 | public Builder setIncludePad(boolean includePad) { |
| 244 | mIncludePad = includePad; |
| 245 | return this; |
| 246 | } |
| 247 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 248 | /** |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 249 | * Set whether to respect the ascent and descent of the fallback fonts that are used in |
| 250 | * displaying the text (which is needed to avoid text from consecutive lines running into |
| 251 | * each other). If set, fallback fonts that end up getting used can increase the ascent |
| 252 | * and descent of the lines that they are used on. |
| 253 | * |
| 254 | * <p>For backward compatibility reasons, the default is {@code false}, but setting this to |
| 255 | * true is strongly recommended. It is required to be true if text could be in languages |
| 256 | * like Burmese or Tibetan where text is typically much taller or deeper than Latin text. |
| 257 | * |
| 258 | * @param useLineSpacingFromFallbacks whether to expand linespacing based on fallback fonts |
| 259 | * @return this builder, useful for chaining |
| 260 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 261 | @NonNull |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 262 | public Builder setUseLineSpacingFromFallbacks(boolean useLineSpacingFromFallbacks) { |
| 263 | mFallbackLineSpacing = useLineSpacingFromFallbacks; |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | /** |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 268 | * Set the width as used for ellipsizing purposes, if it differs from the |
| 269 | * normal layout width. The default is the {@code width} |
| 270 | * passed to {@link #obtain}. |
| 271 | * |
| 272 | * @param ellipsizedWidth width used for ellipsizing, in pixels |
| 273 | * @return this builder, useful for chaining |
| 274 | * @see android.widget.TextView#setEllipsize |
| 275 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 276 | @NonNull |
| 277 | public Builder setEllipsizedWidth(@IntRange(from = 0) int ellipsizedWidth) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 278 | mEllipsizedWidth = ellipsizedWidth; |
| 279 | return this; |
| 280 | } |
| 281 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Set ellipsizing on the layout. Causes words that are longer than the view |
| 284 | * is wide, or exceeding the number of lines (see #setMaxLines) in the case |
| 285 | * of {@link android.text.TextUtils.TruncateAt#END} or |
| 286 | * {@link android.text.TextUtils.TruncateAt#MARQUEE}, to be ellipsized instead |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 287 | * of broken. The default is {@code null}, indicating no ellipsis is to be applied. |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 288 | * |
| 289 | * @param ellipsize type of ellipsis behavior |
| 290 | * @return this builder, useful for chaining |
| 291 | * @see android.widget.TextView#setEllipsize |
| 292 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 293 | @NonNull |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 294 | public Builder setEllipsize(@Nullable TextUtils.TruncateAt ellipsize) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 295 | mEllipsize = ellipsize; |
| 296 | return this; |
| 297 | } |
| 298 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Set maximum number of lines. This is particularly useful in the case of |
| 301 | * ellipsizing, where it changes the layout of the last line. The default is |
| 302 | * unlimited. |
| 303 | * |
| 304 | * @param maxLines maximum number of lines in the layout |
| 305 | * @return this builder, useful for chaining |
| 306 | * @see android.widget.TextView#setMaxLines |
| 307 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 308 | @NonNull |
| 309 | public Builder setMaxLines(@IntRange(from = 0) int maxLines) { |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 310 | mMaxLines = maxLines; |
| 311 | return this; |
| 312 | } |
| 313 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Set break strategy, useful for selecting high quality or balanced paragraph |
| 316 | * layout options. The default is {@link Layout#BREAK_STRATEGY_SIMPLE}. |
Siyamed Sinir | c0dab36 | 2018-07-22 13:48:51 -0700 | [diff] [blame] | 317 | * <p/> |
| 318 | * Enabling hyphenation with either using {@link Layout#HYPHENATION_FREQUENCY_NORMAL} or |
| 319 | * {@link Layout#HYPHENATION_FREQUENCY_FULL} while line breaking is set to one of |
| 320 | * {@link Layout#BREAK_STRATEGY_BALANCED}, {@link Layout#BREAK_STRATEGY_HIGH_QUALITY} |
| 321 | * improves the structure of text layout however has performance impact and requires more |
| 322 | * time to do the text layout. |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 323 | * |
| 324 | * @param breakStrategy break strategy for paragraph layout |
| 325 | * @return this builder, useful for chaining |
| 326 | * @see android.widget.TextView#setBreakStrategy |
Siyamed Sinir | c0dab36 | 2018-07-22 13:48:51 -0700 | [diff] [blame] | 327 | * @see #setHyphenationFrequency(int) |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 328 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 329 | @NonNull |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 330 | public Builder setBreakStrategy(@BreakStrategy int breakStrategy) { |
| 331 | mBreakStrategy = breakStrategy; |
| 332 | return this; |
| 333 | } |
| 334 | |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 335 | /** |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 336 | * Set hyphenation frequency, to control the amount of automatic hyphenation used. The |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 337 | * possible values are defined in {@link Layout}, by constants named with the pattern |
| 338 | * {@code HYPHENATION_FREQUENCY_*}. The default is |
| 339 | * {@link Layout#HYPHENATION_FREQUENCY_NONE}. |
Siyamed Sinir | c0dab36 | 2018-07-22 13:48:51 -0700 | [diff] [blame] | 340 | * <p/> |
| 341 | * Enabling hyphenation with either using {@link Layout#HYPHENATION_FREQUENCY_NORMAL} or |
| 342 | * {@link Layout#HYPHENATION_FREQUENCY_FULL} while line breaking is set to one of |
| 343 | * {@link Layout#BREAK_STRATEGY_BALANCED}, {@link Layout#BREAK_STRATEGY_HIGH_QUALITY} |
| 344 | * improves the structure of text layout however has performance impact and requires more |
| 345 | * time to do the text layout. |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 346 | * |
| 347 | * @param hyphenationFrequency hyphenation frequency for the paragraph |
| 348 | * @return this builder, useful for chaining |
| 349 | * @see android.widget.TextView#setHyphenationFrequency |
Siyamed Sinir | c0dab36 | 2018-07-22 13:48:51 -0700 | [diff] [blame] | 350 | * @see #setBreakStrategy(int) |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 351 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 352 | @NonNull |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 353 | public Builder setHyphenationFrequency(@HyphenationFrequency int hyphenationFrequency) { |
| 354 | mHyphenationFrequency = hyphenationFrequency; |
| 355 | return this; |
| 356 | } |
| 357 | |
| 358 | /** |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 359 | * Set indents. Arguments are arrays holding an indent amount, one per line, measured in |
| 360 | * pixels. For lines past the last element in the array, the last element repeats. |
| 361 | * |
| 362 | * @param leftIndents array of indent values for left margin, in pixels |
| 363 | * @param rightIndents array of indent values for right margin, in pixels |
| 364 | * @return this builder, useful for chaining |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 365 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 366 | @NonNull |
| 367 | public Builder setIndents(@Nullable int[] leftIndents, @Nullable int[] rightIndents) { |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 368 | mLeftIndents = leftIndents; |
| 369 | mRightIndents = rightIndents; |
Raph Levien | e319d5a | 2015-04-14 23:51:07 -0700 | [diff] [blame] | 370 | return this; |
| 371 | } |
| 372 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 373 | /** |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 374 | * Set paragraph justification mode. The default value is |
| 375 | * {@link Layout#JUSTIFICATION_MODE_NONE}. If the last line is too short for justification, |
| 376 | * the last line will be displayed with the alignment set by {@link #setAlignment}. |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 377 | * |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 378 | * @param justificationMode justification mode for the paragraph. |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 379 | * @return this builder, useful for chaining. |
| 380 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 381 | @NonNull |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 382 | public Builder setJustificationMode(@JustificationMode int justificationMode) { |
| 383 | mJustificationMode = justificationMode; |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 384 | return this; |
| 385 | } |
| 386 | |
Siyamed Sinir | 442c151 | 2017-07-24 12:18:27 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Sets whether the line spacing should be applied for the last line. Default value is |
| 389 | * {@code false}. |
| 390 | * |
| 391 | * @hide |
| 392 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 393 | @NonNull |
Siyamed Sinir | 442c151 | 2017-07-24 12:18:27 -0700 | [diff] [blame] | 394 | /* package */ Builder setAddLastLineLineSpacing(boolean value) { |
| 395 | mAddLastLineLineSpacing = value; |
| 396 | return this; |
| 397 | } |
| 398 | |
Seigo Nonaka | 09da71a | 2016-11-28 16:24:14 +0900 | [diff] [blame] | 399 | /** |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 400 | * Build the {@link StaticLayout} after options have been set. |
| 401 | * |
| 402 | * <p>Note: the builder object must not be reused in any way after calling this |
| 403 | * method. Setting parameters after calling this method, or calling it a second |
| 404 | * time on the same builder object, will likely lead to unexpected results. |
| 405 | * |
| 406 | * @return the newly constructed {@link StaticLayout} object |
| 407 | */ |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 408 | @NonNull |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 409 | public StaticLayout build() { |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 410 | StaticLayout result = new StaticLayout(this); |
| 411 | Builder.recycle(this); |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 412 | return result; |
| 413 | } |
| 414 | |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 415 | private CharSequence mText; |
| 416 | private int mStart; |
| 417 | private int mEnd; |
| 418 | private TextPaint mPaint; |
| 419 | private int mWidth; |
| 420 | private Alignment mAlignment; |
| 421 | private TextDirectionHeuristic mTextDir; |
| 422 | private float mSpacingMult; |
| 423 | private float mSpacingAdd; |
| 424 | private boolean mIncludePad; |
| 425 | private boolean mFallbackLineSpacing; |
| 426 | private int mEllipsizedWidth; |
| 427 | private TextUtils.TruncateAt mEllipsize; |
| 428 | private int mMaxLines; |
| 429 | private int mBreakStrategy; |
| 430 | private int mHyphenationFrequency; |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 431 | @Nullable private int[] mLeftIndents; |
| 432 | @Nullable private int[] mRightIndents; |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 433 | private int mJustificationMode; |
| 434 | private boolean mAddLastLineLineSpacing; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 435 | |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 436 | private final Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt(); |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 437 | |
Siyamed Sinir | a273a70 | 2017-10-05 11:22:12 -0700 | [diff] [blame] | 438 | private static final SynchronizedPool<Builder> sPool = new SynchronizedPool<>(3); |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 441 | /** |
| 442 | * @deprecated Use {@link Builder} instead. |
| 443 | */ |
| 444 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | public StaticLayout(CharSequence source, TextPaint paint, |
| 446 | int width, |
| 447 | Alignment align, float spacingmult, float spacingadd, |
| 448 | boolean includepad) { |
| 449 | this(source, 0, source.length(), paint, width, align, |
| 450 | spacingmult, spacingadd, includepad); |
| 451 | } |
| 452 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 453 | /** |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 454 | * @deprecated Use {@link Builder} instead. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 455 | */ |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 456 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 458 | TextPaint paint, int outerwidth, |
| 459 | Alignment align, |
| 460 | float spacingmult, float spacingadd, |
| 461 | boolean includepad) { |
| 462 | this(source, bufstart, bufend, paint, outerwidth, align, |
| 463 | spacingmult, spacingadd, includepad, null, 0); |
| 464 | } |
| 465 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 466 | /** |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 467 | * @deprecated Use {@link Builder} instead. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 468 | */ |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 469 | @Deprecated |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 470 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 471 | TextPaint paint, int outerwidth, |
| 472 | Alignment align, |
| 473 | float spacingmult, float spacingadd, |
| 474 | boolean includepad, |
| 475 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
Seigo Nonaka | 4e90fa2 | 2018-02-13 21:40:01 +0000 | [diff] [blame] | 476 | this(source, bufstart, bufend, paint, outerwidth, align, |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 477 | TextDirectionHeuristics.FIRSTSTRONG_LTR, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 478 | spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @hide |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 483 | * @deprecated Use {@link Builder} instead. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 484 | */ |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 485 | @Deprecated |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 486 | @UnsupportedAppUsage |
Seigo Nonaka | 4e90fa2 | 2018-02-13 21:40:01 +0000 | [diff] [blame] | 487 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | TextPaint paint, int outerwidth, |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 489 | Alignment align, TextDirectionHeuristic textDir, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | float spacingmult, float spacingadd, |
| 491 | boolean includepad, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 492 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | super((ellipsize == null) |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 494 | ? source |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | : (source instanceof Spanned) |
| 496 | ? new SpannedEllipsizer(source) |
| 497 | : new Ellipsizer(source), |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 498 | paint, outerwidth, align, textDir, spacingmult, spacingadd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | |
Raph Levien | ebd66ca | 2015-04-30 15:27:57 -0700 | [diff] [blame] | 500 | Builder b = Builder.obtain(source, bufstart, bufend, paint, outerwidth) |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 501 | .setAlignment(align) |
Raph Levien | a6a0828 | 2015-06-03 13:20:45 -0700 | [diff] [blame] | 502 | .setTextDirection(textDir) |
Raph Levien | 531c30c | 2015-04-30 16:29:59 -0700 | [diff] [blame] | 503 | .setLineSpacing(spacingadd, spacingmult) |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 504 | .setIncludePad(includepad) |
| 505 | .setEllipsizedWidth(ellipsizedWidth) |
| 506 | .setEllipsize(ellipsize) |
| 507 | .setMaxLines(maxLines); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | /* |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 509 | * This is annoying, but we can't refer to the layout until superclass construction is |
| 510 | * finished, and the superclass constructor wants the reference to the display text. |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 511 | * |
Roozbeh Pournader | 22a167c | 2017-08-21 12:53:44 -0700 | [diff] [blame] | 512 | * In other words, the two Ellipsizer classes in Layout.java need a (Dynamic|Static)Layout |
| 513 | * as a parameter to do their calculations, but the Ellipsizers also need to be the input |
| 514 | * to the superclass's constructor (Layout). In order to go around the circular |
| 515 | * dependency, we construct the Ellipsizer with only one of the parameters, the text. And |
| 516 | * we fill in the rest of the needed information (layout, width, and method) later, here. |
| 517 | * |
| 518 | * This will break if the superclass constructor ever actually cares about the content |
| 519 | * instead of just holding the reference. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | */ |
| 521 | if (ellipsize != null) { |
| 522 | Ellipsizer e = (Ellipsizer) getText(); |
| 523 | |
| 524 | e.mLayout = this; |
| 525 | e.mWidth = ellipsizedWidth; |
| 526 | e.mMethod = ellipsize; |
| 527 | mEllipsizedWidth = ellipsizedWidth; |
| 528 | |
| 529 | mColumns = COLUMNS_ELLIPSIZE; |
| 530 | } else { |
| 531 | mColumns = COLUMNS_NORMAL; |
| 532 | mEllipsizedWidth = outerwidth; |
| 533 | } |
| 534 | |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 535 | mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 536 | mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 537 | mMaximumVisibleLineCount = maxLines; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 539 | generate(b, b.mIncludePad, b.mIncludePad); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 540 | |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 541 | Builder.recycle(b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Clara Bayarri | 81a4f2e | 2017-12-19 16:46:56 +0000 | [diff] [blame] | 544 | /** |
| 545 | * Used by DynamicLayout. |
| 546 | */ |
Roozbeh Pournader | e88b5df | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 547 | /* package */ StaticLayout(@Nullable CharSequence text) { |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 548 | super(text, null, 0, null, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | |
| 550 | mColumns = COLUMNS_ELLIPSIZE; |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 551 | mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 552 | mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 555 | private StaticLayout(Builder b) { |
| 556 | super((b.mEllipsize == null) |
| 557 | ? b.mText |
| 558 | : (b.mText instanceof Spanned) |
| 559 | ? new SpannedEllipsizer(b.mText) |
| 560 | : new Ellipsizer(b.mText), |
Roozbeh Pournader | 158dfaf | 2017-09-21 13:23:50 -0700 | [diff] [blame] | 561 | b.mPaint, b.mWidth, b.mAlignment, b.mTextDir, b.mSpacingMult, b.mSpacingAdd); |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 562 | |
| 563 | if (b.mEllipsize != null) { |
| 564 | Ellipsizer e = (Ellipsizer) getText(); |
| 565 | |
| 566 | e.mLayout = this; |
| 567 | e.mWidth = b.mEllipsizedWidth; |
| 568 | e.mMethod = b.mEllipsize; |
| 569 | mEllipsizedWidth = b.mEllipsizedWidth; |
| 570 | |
| 571 | mColumns = COLUMNS_ELLIPSIZE; |
| 572 | } else { |
| 573 | mColumns = COLUMNS_NORMAL; |
| 574 | mEllipsizedWidth = b.mWidth; |
| 575 | } |
| 576 | |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 577 | mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2); |
| 578 | mLines = ArrayUtils.newUnpaddedIntArray(2 * mColumns); |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 579 | mMaximumVisibleLineCount = b.mMaxLines; |
| 580 | |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 581 | mLeftIndents = b.mLeftIndents; |
| 582 | mRightIndents = b.mRightIndents; |
Seigo Nonaka | 4b4730d | 2017-03-31 09:42:16 -0700 | [diff] [blame] | 583 | setJustificationMode(b.mJustificationMode); |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 584 | |
Raph Levien | 39b4db7 | 2015-03-25 13:18:20 -0700 | [diff] [blame] | 585 | generate(b, b.mIncludePad, b.mIncludePad); |
| 586 | } |
| 587 | |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 588 | /* package */ void generate(Builder b, boolean includepad, boolean trackpad) { |
Roozbeh Pournader | e88b5df | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 589 | final CharSequence source = b.mText; |
Seigo Nonaka | fbe63bd | 2017-12-02 19:28:05 -0800 | [diff] [blame] | 590 | final int bufStart = b.mStart; |
| 591 | final int bufEnd = b.mEnd; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 592 | TextPaint paint = b.mPaint; |
| 593 | int outerWidth = b.mWidth; |
| 594 | TextDirectionHeuristic textDir = b.mTextDir; |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 595 | final boolean fallbackLineSpacing = b.mFallbackLineSpacing; |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 596 | float spacingmult = b.mSpacingMult; |
| 597 | float spacingadd = b.mSpacingAdd; |
| 598 | float ellipsizedWidth = b.mEllipsizedWidth; |
| 599 | TextUtils.TruncateAt ellipsize = b.mEllipsize; |
Siyamed Sinir | 442c151 | 2017-07-24 12:18:27 -0700 | [diff] [blame] | 600 | final boolean addLastLineSpacing = b.mAddLastLineLineSpacing; |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 601 | NativeLineBreaker.LineBreaks lineBreaks = new NativeLineBreaker.LineBreaks(); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 602 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | mLineCount = 0; |
Siyamed Sinir | a19cd51 | 2017-08-03 22:01:56 -0700 | [diff] [blame] | 604 | mEllipsized = false; |
Siyamed Sinir | a8d982d | 2017-08-21 13:27:47 -0700 | [diff] [blame] | 605 | mMaxLineHeight = mMaximumVisibleLineCount < 1 ? 0 : DEFAULT_MAX_LINE_HEIGHT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | |
| 607 | int v = 0; |
| 608 | boolean needMultiply = (spacingmult != 1 || spacingadd != 0); |
| 609 | |
Raph Levien | d3ab692 | 2015-03-02 14:30:53 -0800 | [diff] [blame] | 610 | Paint.FontMetricsInt fm = b.mFontMetricsInt; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 611 | int[] chooseHtv = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 613 | final int[] indents; |
| 614 | if (mLeftIndents != null || mRightIndents != null) { |
| 615 | final int leftLen = mLeftIndents == null ? 0 : mLeftIndents.length; |
| 616 | final int rightLen = mRightIndents == null ? 0 : mRightIndents.length; |
| 617 | final int indentsLen = Math.max(leftLen, rightLen); |
| 618 | indents = new int[indentsLen]; |
| 619 | for (int i = 0; i < leftLen; i++) { |
| 620 | indents[i] = mLeftIndents[i]; |
| 621 | } |
| 622 | for (int i = 0; i < rightLen; i++) { |
| 623 | indents[i] += mRightIndents[i]; |
| 624 | } |
| 625 | } else { |
| 626 | indents = null; |
| 627 | } |
| 628 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 629 | final NativeLineBreaker lineBreaker = new NativeLineBreaker( |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 630 | b.mBreakStrategy, b.mHyphenationFrequency, |
| 631 | // TODO: Support more justification mode, e.g. letter spacing, stretching. |
| 632 | b.mJustificationMode != Layout.JUSTIFICATION_MODE_NONE, |
Seigo Nonaka | 9155fa6 | 2018-06-05 18:22:03 -0700 | [diff] [blame] | 633 | indents); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | |
Seigo Nonaka | c3328d6 | 2018-03-20 15:18:59 -0700 | [diff] [blame] | 635 | PrecomputedText.ParagraphInfo[] paragraphInfo = null; |
| 636 | final Spanned spanned = (source instanceof Spanned) ? (Spanned) source : null; |
Seigo Nonaka | beafa1f | 2018-02-01 21:39:24 -0800 | [diff] [blame] | 637 | if (source instanceof PrecomputedText) { |
Seigo Nonaka | c3328d6 | 2018-03-20 15:18:59 -0700 | [diff] [blame] | 638 | PrecomputedText precomputed = (PrecomputedText) source; |
| 639 | if (precomputed.canUseMeasuredResult(bufStart, bufEnd, textDir, paint, |
| 640 | b.mBreakStrategy, b.mHyphenationFrequency)) { |
Seigo Nonaka | beafa1f | 2018-02-01 21:39:24 -0800 | [diff] [blame] | 641 | // Some parameters are different from the ones when measured text is created. |
Seigo Nonaka | c3328d6 | 2018-03-20 15:18:59 -0700 | [diff] [blame] | 642 | paragraphInfo = precomputed.getParagraphInfo(); |
Seigo Nonaka | fbe63bd | 2017-12-02 19:28:05 -0800 | [diff] [blame] | 643 | } |
Seigo Nonaka | 87b1547 | 2018-01-12 14:06:29 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Seigo Nonaka | c3328d6 | 2018-03-20 15:18:59 -0700 | [diff] [blame] | 646 | if (paragraphInfo == null) { |
Seigo Nonaka | beafa1f | 2018-02-01 21:39:24 -0800 | [diff] [blame] | 647 | final PrecomputedText.Params param = new PrecomputedText.Params(paint, textDir, |
| 648 | b.mBreakStrategy, b.mHyphenationFrequency); |
Seigo Nonaka | c3328d6 | 2018-03-20 15:18:59 -0700 | [diff] [blame] | 649 | paragraphInfo = PrecomputedText.createMeasuredParagraphs(source, param, bufStart, |
| 650 | bufEnd, false /* computeLayout */); |
Seigo Nonaka | 4e90fa2 | 2018-02-13 21:40:01 +0000 | [diff] [blame] | 651 | } |
Seigo Nonaka | fbe63bd | 2017-12-02 19:28:05 -0800 | [diff] [blame] | 652 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 653 | for (int paraIndex = 0; paraIndex < paragraphInfo.length; paraIndex++) { |
| 654 | final int paraStart = paraIndex == 0 |
| 655 | ? bufStart : paragraphInfo[paraIndex - 1].paragraphEnd; |
| 656 | final int paraEnd = paragraphInfo[paraIndex].paragraphEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 658 | int firstWidthLineCount = 1; |
| 659 | int firstWidth = outerWidth; |
| 660 | int restWidth = outerWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 662 | LineHeightSpan[] chooseHt = null; |
| 663 | if (spanned != null) { |
| 664 | LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd, |
| 665 | LeadingMarginSpan.class); |
| 666 | for (int i = 0; i < sp.length; i++) { |
| 667 | LeadingMarginSpan lms = sp[i]; |
| 668 | firstWidth -= sp[i].getLeadingMargin(true); |
| 669 | restWidth -= sp[i].getLeadingMargin(false); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 670 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 671 | // LeadingMarginSpan2 is odd. The count affects all |
| 672 | // leading margin spans, not just this particular one |
| 673 | if (lms instanceof LeadingMarginSpan2) { |
| 674 | LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms; |
| 675 | firstWidthLineCount = Math.max(firstWidthLineCount, |
| 676 | lms2.getLeadingMarginLineCount()); |
| 677 | } |
| 678 | } |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 679 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 680 | chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class); |
| 681 | |
| 682 | if (chooseHt.length == 0) { |
| 683 | chooseHt = null; // So that out() would not assume it has any contents |
| 684 | } else { |
| 685 | if (chooseHtv == null || chooseHtv.length < chooseHt.length) { |
| 686 | chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 689 | for (int i = 0; i < chooseHt.length; i++) { |
| 690 | int o = spanned.getSpanStart(chooseHt[i]); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 691 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 692 | if (o < paraStart) { |
| 693 | // starts in this layout, before the |
| 694 | // current paragraph |
| 695 | |
| 696 | chooseHtv[i] = getLineTop(getLineForOffset(o)); |
| 697 | } else { |
| 698 | // starts in this paragraph |
| 699 | |
| 700 | chooseHtv[i] = v; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | // tab stop locations |
| 706 | int[] variableTabStops = null; |
| 707 | if (spanned != null) { |
| 708 | TabStopSpan[] spans = getParagraphSpans(spanned, paraStart, |
| 709 | paraEnd, TabStopSpan.class); |
| 710 | if (spans.length > 0) { |
| 711 | int[] stops = new int[spans.length]; |
| 712 | for (int i = 0; i < spans.length; i++) { |
| 713 | stops[i] = spans[i].getTabStop(); |
| 714 | } |
| 715 | Arrays.sort(stops, 0, stops.length); |
| 716 | variableTabStops = stops; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | final MeasuredParagraph measuredPara = paragraphInfo[paraIndex].measured; |
| 721 | final char[] chs = measuredPara.getChars(); |
| 722 | final int[] spanEndCache = measuredPara.getSpanEndCache().getRawArray(); |
| 723 | final int[] fmCache = measuredPara.getFontMetrics().getRawArray(); |
| 724 | int breakCount = lineBreaker.computeLineBreaks( |
| 725 | measuredPara.getChars(), |
| 726 | measuredPara.getNativeMeasuredParagraph(), |
| 727 | paraEnd - paraStart, |
| 728 | firstWidth, |
| 729 | firstWidthLineCount, |
| 730 | restWidth, |
| 731 | variableTabStops, |
| 732 | TAB_INCREMENT, |
| 733 | mLineCount, |
| 734 | lineBreaks); |
| 735 | |
| 736 | final int[] breaks = lineBreaks.breaks; |
| 737 | final float[] lineWidths = lineBreaks.widths; |
| 738 | final float[] ascents = lineBreaks.ascents; |
| 739 | final float[] descents = lineBreaks.descents; |
| 740 | final int[] flags = lineBreaks.flags; |
| 741 | |
| 742 | final int remainingLineCount = mMaximumVisibleLineCount - mLineCount; |
| 743 | final boolean ellipsisMayBeApplied = ellipsize != null |
| 744 | && (ellipsize == TextUtils.TruncateAt.END |
| 745 | || (mMaximumVisibleLineCount == 1 |
| 746 | && ellipsize != TextUtils.TruncateAt.MARQUEE)); |
| 747 | if (0 < remainingLineCount && remainingLineCount < breakCount |
| 748 | && ellipsisMayBeApplied) { |
| 749 | // Calculate width |
| 750 | float width = 0; |
| 751 | int flag = 0; // XXX May need to also have starting hyphen edit |
| 752 | for (int i = remainingLineCount - 1; i < breakCount; i++) { |
| 753 | if (i == breakCount - 1) { |
| 754 | width += lineWidths[i]; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 755 | } else { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 756 | for (int j = (i == 0 ? 0 : breaks[i - 1]); j < breaks[i]; j++) { |
| 757 | width += measuredPara.getCharWidthAt(j - paraStart); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 758 | } |
Mark Wagner | 7b5676e | 2009-10-16 11:44:23 -0700 | [diff] [blame] | 759 | } |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 760 | flag |= flags[i] & TAB_MASK; |
| 761 | } |
| 762 | // Treat the last line and overflowed lines as a single line. |
| 763 | breaks[remainingLineCount - 1] = breaks[breakCount - 1]; |
| 764 | lineWidths[remainingLineCount - 1] = width; |
| 765 | flags[remainingLineCount - 1] = flag; |
| 766 | |
| 767 | breakCount = remainingLineCount; |
| 768 | } |
| 769 | |
| 770 | // here is the offset of the starting character of the line we are currently |
| 771 | // measuring |
| 772 | int here = paraStart; |
| 773 | |
| 774 | int fmTop = 0, fmBottom = 0, fmAscent = 0, fmDescent = 0; |
| 775 | int fmCacheIndex = 0; |
| 776 | int spanEndCacheIndex = 0; |
| 777 | int breakIndex = 0; |
| 778 | for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) { |
| 779 | // retrieve end of span |
| 780 | spanEnd = spanEndCache[spanEndCacheIndex++]; |
| 781 | |
| 782 | // retrieve cached metrics, order matches above |
| 783 | fm.top = fmCache[fmCacheIndex * 4 + 0]; |
| 784 | fm.bottom = fmCache[fmCacheIndex * 4 + 1]; |
| 785 | fm.ascent = fmCache[fmCacheIndex * 4 + 2]; |
| 786 | fm.descent = fmCache[fmCacheIndex * 4 + 3]; |
| 787 | fmCacheIndex++; |
| 788 | |
| 789 | if (fm.top < fmTop) { |
| 790 | fmTop = fm.top; |
| 791 | } |
| 792 | if (fm.ascent < fmAscent) { |
| 793 | fmAscent = fm.ascent; |
| 794 | } |
| 795 | if (fm.descent > fmDescent) { |
| 796 | fmDescent = fm.descent; |
| 797 | } |
| 798 | if (fm.bottom > fmBottom) { |
| 799 | fmBottom = fm.bottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 802 | // skip breaks ending before current span range |
| 803 | while (breakIndex < breakCount && paraStart + breaks[breakIndex] < spanStart) { |
| 804 | breakIndex++; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 807 | while (breakIndex < breakCount && paraStart + breaks[breakIndex] <= spanEnd) { |
| 808 | int endPos = paraStart + breaks[breakIndex]; |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 809 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 810 | boolean moreChars = (endPos < bufEnd); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 811 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 812 | final int ascent = fallbackLineSpacing |
| 813 | ? Math.min(fmAscent, Math.round(ascents[breakIndex])) |
| 814 | : fmAscent; |
| 815 | final int descent = fallbackLineSpacing |
| 816 | ? Math.max(fmDescent, Math.round(descents[breakIndex])) |
| 817 | : fmDescent; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 818 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 819 | v = out(source, here, endPos, |
| 820 | ascent, descent, fmTop, fmBottom, |
| 821 | v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, |
| 822 | flags[breakIndex], needMultiply, measuredPara, bufEnd, |
| 823 | includepad, trackpad, addLastLineSpacing, chs, |
| 824 | paraStart, ellipsize, ellipsizedWidth, lineWidths[breakIndex], |
| 825 | paint, moreChars); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 826 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 827 | if (endPos < spanEnd) { |
| 828 | // preserve metrics for current span |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 829 | fmTop = fm.top; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 830 | fmBottom = fm.bottom; |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 831 | fmAscent = fm.ascent; |
| 832 | fmDescent = fm.descent; |
| 833 | } else { |
| 834 | fmTop = fmBottom = fmAscent = fmDescent = 0; |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 837 | here = endPos; |
| 838 | breakIndex++; |
| 839 | |
| 840 | if (mLineCount >= mMaximumVisibleLineCount && mEllipsized) { |
| 841 | return; |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 846 | if (paraEnd == bufEnd) { |
| 847 | break; |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 848 | } |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) |
| 852 | && mLineCount < mMaximumVisibleLineCount) { |
| 853 | final MeasuredParagraph measuredPara = |
| 854 | MeasuredParagraph.buildForBidi(source, bufEnd, bufEnd, textDir, null); |
| 855 | paint.getFontMetricsInt(fm); |
| 856 | v = out(source, |
| 857 | bufEnd, bufEnd, fm.ascent, fm.descent, |
| 858 | fm.top, fm.bottom, |
| 859 | v, |
| 860 | spacingmult, spacingadd, null, |
| 861 | null, fm, 0, |
| 862 | needMultiply, measuredPara, bufEnd, |
| 863 | includepad, trackpad, addLastLineSpacing, null, |
| 864 | bufStart, ellipsize, |
| 865 | ellipsizedWidth, 0, paint, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Roozbeh Pournader | e88b5df | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 869 | private int out(final CharSequence text, final int start, final int end, int above, int below, |
| 870 | int top, int bottom, int v, final float spacingmult, final float spacingadd, |
| 871 | final LineHeightSpan[] chooseHt, final int[] chooseHtv, final Paint.FontMetricsInt fm, |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 872 | final int flags, final boolean needMultiply, @NonNull final MeasuredParagraph measured, |
Seigo Nonaka | f1644f7 | 2017-11-27 22:09:49 -0800 | [diff] [blame] | 873 | final int bufEnd, final boolean includePad, final boolean trackPad, |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 874 | final boolean addLastLineLineSpacing, final char[] chs, |
Roozbeh Pournader | e88b5df | 2017-07-25 13:52:57 -0700 | [diff] [blame] | 875 | final int widthStart, final TextUtils.TruncateAt ellipsize, final float ellipsisWidth, |
| 876 | final float textWidth, final TextPaint paint, final boolean moreChars) { |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 877 | final int j = mLineCount; |
| 878 | final int off = j * mColumns; |
| 879 | final int want = off + mColumns + TOP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | int[] lines = mLines; |
Seigo Nonaka | 4b6bcee | 2017-12-11 11:39:51 -0800 | [diff] [blame] | 881 | final int dir = measured.getParagraphDir(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | if (want >= lines.length) { |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 884 | final int[] grow = ArrayUtils.newUnpaddedIntArray(GrowingArrayUtils.growSize(want)); |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 885 | System.arraycopy(lines, 0, grow, 0, lines.length); |
| 886 | mLines = grow; |
| 887 | lines = grow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Siyamed Sinir | af39851 | 2017-07-25 19:08:42 -0700 | [diff] [blame] | 890 | if (j >= mLineDirections.length) { |
| 891 | final Directions[] grow = ArrayUtils.newUnpaddedArray(Directions.class, |
| 892 | GrowingArrayUtils.growSize(j)); |
| 893 | System.arraycopy(mLineDirections, 0, grow, 0, mLineDirections.length); |
| 894 | mLineDirections = grow; |
| 895 | } |
| 896 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 897 | if (chooseHt != null) { |
| 898 | fm.ascent = above; |
| 899 | fm.descent = below; |
| 900 | fm.top = top; |
| 901 | fm.bottom = bottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 903 | for (int i = 0; i < chooseHt.length; i++) { |
| 904 | if (chooseHt[i] instanceof LineHeightSpan.WithDensity) { |
| 905 | ((LineHeightSpan.WithDensity) chooseHt[i]) |
| 906 | .chooseHeight(text, start, end, chooseHtv[i], v, fm, paint); |
| 907 | } else { |
| 908 | chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm); |
| 909 | } |
| 910 | } |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 911 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 912 | above = fm.ascent; |
| 913 | below = fm.descent; |
| 914 | top = fm.top; |
| 915 | bottom = fm.bottom; |
| 916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 918 | boolean firstLine = (j == 0); |
| 919 | boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount); |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 920 | |
| 921 | if (ellipsize != null) { |
| 922 | // If there is only one line, then do any type of ellipsis except when it is MARQUEE |
| 923 | // if there are multiple lines, just allow END ellipsis on the last line |
| 924 | boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount); |
| 925 | |
| 926 | boolean doEllipsis = |
| 927 | (((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) && |
| 928 | ellipsize != TextUtils.TruncateAt.MARQUEE) || |
| 929 | (!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) && |
| 930 | ellipsize == TextUtils.TruncateAt.END); |
| 931 | if (doEllipsis) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 932 | calculateEllipsis(start, end, measured, widthStart, |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 933 | ellipsisWidth, ellipsize, j, |
| 934 | textWidth, paint, forceEllipsis); |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Siyamed Sinir | fb0b2dc | 2017-07-26 22:08:24 -0700 | [diff] [blame] | 938 | final boolean lastLine; |
| 939 | if (mEllipsized) { |
| 940 | lastLine = true; |
| 941 | } else { |
| 942 | final boolean lastCharIsNewLine = widthStart != bufEnd && bufEnd > 0 |
| 943 | && text.charAt(bufEnd - 1) == CHAR_NEW_LINE; |
| 944 | if (end == bufEnd && !lastCharIsNewLine) { |
| 945 | lastLine = true; |
| 946 | } else if (start == bufEnd && lastCharIsNewLine) { |
| 947 | lastLine = true; |
| 948 | } else { |
| 949 | lastLine = false; |
| 950 | } |
| 951 | } |
Raph Levien | d97b097 | 2014-04-24 12:51:35 -0700 | [diff] [blame] | 952 | |
| 953 | if (firstLine) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 954 | if (trackPad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | mTopPadding = top - above; |
| 956 | } |
| 957 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 958 | if (includePad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | above = top; |
| 960 | } |
| 961 | } |
Raph Levien | d97b097 | 2014-04-24 12:51:35 -0700 | [diff] [blame] | 962 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 963 | int extra; |
| 964 | |
Raph Levien | d97b097 | 2014-04-24 12:51:35 -0700 | [diff] [blame] | 965 | if (lastLine) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 966 | if (trackPad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | mBottomPadding = bottom - below; |
| 968 | } |
| 969 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 970 | if (includePad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | below = bottom; |
| 972 | } |
| 973 | } |
| 974 | |
Siyamed Sinir | 442c151 | 2017-07-24 12:18:27 -0700 | [diff] [blame] | 975 | if (needMultiply && (addLastLineLineSpacing || !lastLine)) { |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 976 | double ex = (below - above) * (spacingmult - 1) + spacingadd; |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 977 | if (ex >= 0) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 978 | extra = (int)(ex + EXTRA_ROUNDING); |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 979 | } else { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 980 | extra = -(int)(-ex + EXTRA_ROUNDING); |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 981 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | } else { |
| 983 | extra = 0; |
| 984 | } |
| 985 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 986 | lines[off + START] = start; |
| 987 | lines[off + TOP] = v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | lines[off + DESCENT] = below + extra; |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 989 | lines[off + EXTRA] = extra; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 991 | // special case for non-ellipsized last visible line when maxLines is set |
| 992 | // store the height as if it was ellipsized |
| 993 | if (!mEllipsized && currentLineIsTheLastVisibleOne) { |
| 994 | // below calculation as if it was the last line |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 995 | int maxLineBelow = includePad ? bottom : below; |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 996 | // similar to the calculation of v below, without the extra. |
| 997 | mMaxLineHeight = v + (maxLineBelow - above); |
| 998 | } |
| 999 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | v += (below - above) + extra; |
| 1001 | lines[off + mColumns + START] = end; |
| 1002 | lines[off + mColumns + TOP] = v; |
| 1003 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1004 | // TODO: could move TAB to share same column as HYPHEN, simplifying this code and gaining |
| 1005 | // one bit for start field |
| 1006 | lines[off + TAB] |= flags & TAB_MASK; |
| 1007 | lines[off + HYPHEN] = flags; |
| 1008 | lines[off + DIR] |= dir << DIR_SHIFT; |
| 1009 | mLineDirections[j] = measured.getDirections(start - widthStart, end - widthStart); |
| 1010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | mLineCount++; |
| 1012 | return v; |
| 1013 | } |
| 1014 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1015 | private void calculateEllipsis(int lineStart, int lineEnd, |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1016 | MeasuredParagraph measured, int widthStart, |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1017 | float avail, TextUtils.TruncateAt where, |
| 1018 | int line, float textWidth, TextPaint paint, |
| 1019 | boolean forceEllipsis) { |
| 1020 | avail -= getTotalInsets(line); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1021 | if (textWidth <= avail && !forceEllipsis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | // Everything fits! |
| 1023 | mLines[mColumns * line + ELLIPSIS_START] = 0; |
| 1024 | mLines[mColumns * line + ELLIPSIS_COUNT] = 0; |
| 1025 | return; |
| 1026 | } |
| 1027 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1028 | float ellipsisWidth = paint.measureText(TextUtils.getEllipsisString(where)); |
| 1029 | int ellipsisStart = 0; |
| 1030 | int ellipsisCount = 0; |
| 1031 | int len = lineEnd - lineStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1033 | // We only support start ellipsis on a single line |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (where == TextUtils.TruncateAt.START) { |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1035 | if (mMaximumVisibleLineCount == 1) { |
| 1036 | float sum = 0; |
| 1037 | int i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | |
Keisuke Kuroyanagi | ed2eea1 | 2015-04-14 18:18:35 +0900 | [diff] [blame] | 1039 | for (i = len; i > 0; i--) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1040 | float w = measured.getCharWidthAt(i - 1 + lineStart - widthStart); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1041 | if (w + sum + ellipsisWidth > avail) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1042 | while (i < len |
| 1043 | && measured.getCharWidthAt(i + lineStart - widthStart) == 0.0f) { |
Keisuke Kuroyanagi | 82d1c44 | 2016-09-14 14:30:14 +0900 | [diff] [blame] | 1044 | i++; |
| 1045 | } |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | sum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1052 | ellipsisStart = 0; |
| 1053 | ellipsisCount = i; |
| 1054 | } else { |
| 1055 | if (Log.isLoggable(TAG, Log.WARN)) { |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1056 | Log.w(TAG, "Start Ellipsis only supported with one line"); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1057 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | } |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 1059 | } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE || |
| 1060 | where == TextUtils.TruncateAt.END_SMALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | float sum = 0; |
| 1062 | int i; |
| 1063 | |
| 1064 | for (i = 0; i < len; i++) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1065 | float w = measured.getCharWidthAt(i + lineStart - widthStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 1067 | if (w + sum + ellipsisWidth > avail) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | sum += w; |
| 1072 | } |
| 1073 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1074 | ellipsisStart = i; |
| 1075 | ellipsisCount = len - i; |
| 1076 | if (forceEllipsis && ellipsisCount == 0 && len > 0) { |
Fabrice Di Meglio | aef455f | 2011-08-29 15:39:11 -0700 | [diff] [blame] | 1077 | ellipsisStart = len - 1; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1078 | ellipsisCount = 1; |
| 1079 | } |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1080 | } else { |
| 1081 | // where = TextUtils.TruncateAt.MIDDLE We only support middle ellipsis on a single line |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1082 | if (mMaximumVisibleLineCount == 1) { |
| 1083 | float lsum = 0, rsum = 0; |
| 1084 | int left = 0, right = len; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1086 | float ravail = (avail - ellipsisWidth) / 2; |
Raph Levien | 0e3c5e8 | 2014-12-04 13:26:07 -0800 | [diff] [blame] | 1087 | for (right = len; right > 0; right--) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1088 | float w = measured.getCharWidthAt(right - 1 + lineStart - widthStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1090 | if (w + rsum > ravail) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1091 | while (right < len |
| 1092 | && measured.getCharWidthAt(right + lineStart - widthStart) |
| 1093 | == 0.0f) { |
Keisuke Kuroyanagi | 82d1c44 | 2016-09-14 14:30:14 +0900 | [diff] [blame] | 1094 | right++; |
| 1095 | } |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1096 | break; |
| 1097 | } |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1098 | rsum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1101 | float lavail = avail - ellipsisWidth - rsum; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1102 | for (left = 0; left < right; left++) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1103 | float w = measured.getCharWidthAt(left + lineStart - widthStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1105 | if (w + lsum > lavail) { |
| 1106 | break; |
| 1107 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1109 | lsum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1112 | ellipsisStart = left; |
| 1113 | ellipsisCount = right - left; |
| 1114 | } else { |
| 1115 | if (Log.isLoggable(TAG, Log.WARN)) { |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1116 | Log.w(TAG, "Middle Ellipsis only supported with one line"); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } |
Siyamed Sinir | 1a5648a | 2018-01-23 16:40:06 -0800 | [diff] [blame] | 1120 | mEllipsized = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart; |
| 1122 | mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount; |
| 1123 | } |
| 1124 | |
Selim Cinek | 365ec09 | 2017-03-09 00:10:52 -0800 | [diff] [blame] | 1125 | private float getTotalInsets(int line) { |
| 1126 | int totalIndent = 0; |
| 1127 | if (mLeftIndents != null) { |
| 1128 | totalIndent = mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1129 | } |
| 1130 | if (mRightIndents != null) { |
| 1131 | totalIndent += mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1132 | } |
| 1133 | return totalIndent; |
| 1134 | } |
| 1135 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 1136 | // Override the base class so we can directly access our members, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | // rather than relying on member functions. |
| 1138 | // The logic mirrors that of Layout.getLineForVertical |
| 1139 | // FIXME: It may be faster to do a linear search for layouts without many lines. |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1140 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | public int getLineForVertical(int vertical) { |
| 1142 | int high = mLineCount; |
| 1143 | int low = -1; |
| 1144 | int guess; |
| 1145 | int[] lines = mLines; |
| 1146 | while (high - low > 1) { |
| 1147 | guess = (high + low) >> 1; |
| 1148 | if (lines[mColumns * guess + TOP] > vertical){ |
| 1149 | high = guess; |
| 1150 | } else { |
| 1151 | low = guess; |
| 1152 | } |
| 1153 | } |
| 1154 | if (low < 0) { |
| 1155 | return 0; |
| 1156 | } else { |
| 1157 | return low; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1161 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | public int getLineCount() { |
| 1163 | return mLineCount; |
| 1164 | } |
| 1165 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1166 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | public int getLineTop(int line) { |
Raph Levien | 07e6c23 | 2016-04-04 12:34:06 -0700 | [diff] [blame] | 1168 | return mLines[mColumns * line + TOP]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1171 | /** |
| 1172 | * @hide |
| 1173 | */ |
| 1174 | @Override |
| 1175 | public int getLineExtra(int line) { |
| 1176 | return mLines[mColumns * line + EXTRA]; |
| 1177 | } |
| 1178 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1179 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | public int getLineDescent(int line) { |
Raph Levien | 07e6c23 | 2016-04-04 12:34:06 -0700 | [diff] [blame] | 1181 | return mLines[mColumns * line + DESCENT]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1184 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | public int getLineStart(int line) { |
| 1186 | return mLines[mColumns * line + START] & START_MASK; |
| 1187 | } |
| 1188 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1189 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | public int getParagraphDirection(int line) { |
| 1191 | return mLines[mColumns * line + DIR] >> DIR_SHIFT; |
| 1192 | } |
| 1193 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1194 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | public boolean getLineContainsTab(int line) { |
| 1196 | return (mLines[mColumns * line + TAB] & TAB_MASK) != 0; |
| 1197 | } |
| 1198 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1199 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | public final Directions getLineDirections(int line) { |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1201 | if (line > getLineCount()) { |
| 1202 | throw new ArrayIndexOutOfBoundsException(); |
| 1203 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | return mLineDirections[line]; |
| 1205 | } |
| 1206 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1207 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | public int getTopPadding() { |
| 1209 | return mTopPadding; |
| 1210 | } |
| 1211 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 1212 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | public int getBottomPadding() { |
| 1214 | return mBottomPadding; |
| 1215 | } |
| 1216 | |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 1217 | /** |
| 1218 | * @hide |
| 1219 | */ |
| 1220 | @Override |
| 1221 | public int getHyphen(int line) { |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 1222 | return mLines[mColumns * line + HYPHEN] & HYPHEN_MASK; |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Raph Levien | 2ea5290 | 2015-07-01 14:39:31 -0700 | [diff] [blame] | 1225 | /** |
| 1226 | * @hide |
| 1227 | */ |
| 1228 | @Override |
| 1229 | public int getIndentAdjust(int line, Alignment align) { |
| 1230 | if (align == Alignment.ALIGN_LEFT) { |
| 1231 | if (mLeftIndents == null) { |
| 1232 | return 0; |
| 1233 | } else { |
| 1234 | return mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1235 | } |
| 1236 | } else if (align == Alignment.ALIGN_RIGHT) { |
| 1237 | if (mRightIndents == null) { |
| 1238 | return 0; |
| 1239 | } else { |
| 1240 | return -mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1241 | } |
| 1242 | } else if (align == Alignment.ALIGN_CENTER) { |
| 1243 | int left = 0; |
| 1244 | if (mLeftIndents != null) { |
| 1245 | left = mLeftIndents[Math.min(line, mLeftIndents.length - 1)]; |
| 1246 | } |
| 1247 | int right = 0; |
| 1248 | if (mRightIndents != null) { |
| 1249 | right = mRightIndents[Math.min(line, mRightIndents.length - 1)]; |
| 1250 | } |
| 1251 | return (left - right) >> 1; |
| 1252 | } else { |
| 1253 | throw new AssertionError("unhandled alignment " + align); |
| 1254 | } |
| 1255 | } |
| 1256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | @Override |
| 1258 | public int getEllipsisCount(int line) { |
| 1259 | if (mColumns < COLUMNS_ELLIPSIZE) { |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | return mLines[mColumns * line + ELLIPSIS_COUNT]; |
| 1264 | } |
| 1265 | |
| 1266 | @Override |
| 1267 | public int getEllipsisStart(int line) { |
| 1268 | if (mColumns < COLUMNS_ELLIPSIZE) { |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | return mLines[mColumns * line + ELLIPSIS_START]; |
| 1273 | } |
| 1274 | |
| 1275 | @Override |
| 1276 | public int getEllipsizedWidth() { |
| 1277 | return mEllipsizedWidth; |
| 1278 | } |
| 1279 | |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1280 | /** |
| 1281 | * Return the total height of this layout. |
| 1282 | * |
| 1283 | * @param cap if true and max lines is set, returns the height of the layout at the max lines. |
| 1284 | * |
| 1285 | * @hide |
| 1286 | */ |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1287 | @UnsupportedAppUsage |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1288 | public int getHeight(boolean cap) { |
Siyamed Sinir | 70ffd28 | 2018-03-08 17:19:46 -0800 | [diff] [blame] | 1289 | if (cap && mLineCount > mMaximumVisibleLineCount && mMaxLineHeight == -1 |
| 1290 | && Log.isLoggable(TAG, Log.WARN)) { |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1291 | Log.w(TAG, "maxLineHeight should not be -1. " |
| 1292 | + " maxLines:" + mMaximumVisibleLineCount |
| 1293 | + " lineCount:" + mLineCount); |
| 1294 | } |
| 1295 | |
Siyamed Sinir | 70ffd28 | 2018-03-08 17:19:46 -0800 | [diff] [blame] | 1296 | return cap && mLineCount > mMaximumVisibleLineCount && mMaxLineHeight != -1 |
| 1297 | ? mMaxLineHeight : super.getHeight(); |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1300 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | private int mLineCount; |
| 1302 | private int mTopPadding, mBottomPadding; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1303 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | private int mColumns; |
| 1305 | private int mEllipsizedWidth; |
| 1306 | |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1307 | /** |
| 1308 | * Keeps track if ellipsize is applied to the text. |
| 1309 | */ |
| 1310 | private boolean mEllipsized; |
| 1311 | |
| 1312 | /** |
| 1313 | * If maxLines is set, ellipsize is not set, and the actual line count of text is greater than |
| 1314 | * or equal to maxLine, this variable holds the ideal visual height of the maxLine'th line |
| 1315 | * starting from the top of the layout. If maxLines is not set its value will be -1. |
| 1316 | * |
| 1317 | * The value is the same as getLineTop(maxLines) for ellipsized version where structurally no |
| 1318 | * more than maxLines is contained. |
| 1319 | */ |
Siyamed Sinir | a19cd51 | 2017-08-03 22:01:56 -0700 | [diff] [blame] | 1320 | private int mMaxLineHeight = DEFAULT_MAX_LINE_HEIGHT; |
Siyamed Sinir | 0745c72 | 2016-05-31 20:39:33 -0700 | [diff] [blame] | 1321 | |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1322 | private static final int COLUMNS_NORMAL = 5; |
| 1323 | private static final int COLUMNS_ELLIPSIZE = 7; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | private static final int START = 0; |
| 1325 | private static final int DIR = START; |
| 1326 | private static final int TAB = START; |
| 1327 | private static final int TOP = 1; |
| 1328 | private static final int DESCENT = 2; |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1329 | private static final int EXTRA = 3; |
| 1330 | private static final int HYPHEN = 4; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1331 | @UnsupportedAppUsage |
Siyamed Sinir | 0fa89d6 | 2017-07-24 20:46:41 -0700 | [diff] [blame] | 1332 | private static final int ELLIPSIS_START = 5; |
| 1333 | private static final int ELLIPSIS_COUNT = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1335 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | private int[] mLines; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1337 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | private Directions[] mLineDirections; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1339 | @UnsupportedAppUsage |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 1340 | private int mMaximumVisibleLineCount = Integer.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | |
| 1342 | private static final int START_MASK = 0x1FFFFFFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | private static final int DIR_SHIFT = 30; |
| 1344 | private static final int TAB_MASK = 0x20000000; |
Keisuke Kuroyanagi | f5af4a3 | 2016-08-31 21:40:53 +0900 | [diff] [blame] | 1345 | private static final int HYPHEN_MASK = 0xFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 1347 | private static final int TAB_INCREMENT = 20; // same as Layout, but that's private |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 1349 | private static final char CHAR_NEW_LINE = '\n'; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 1350 | |
| 1351 | private static final double EXTRA_ROUNDING = 0.5; |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 1352 | |
Siyamed Sinir | a19cd51 | 2017-08-03 22:01:56 -0700 | [diff] [blame] | 1353 | private static final int DEFAULT_MAX_LINE_HEIGHT = -1; |
| 1354 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 1355 | // Unused, here because of gray list private API accesses. |
Deepanshu Gupta | 7053919 | 2014-10-15 15:57:40 -0700 | [diff] [blame] | 1356 | /*package*/ static class LineBreaks { |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 1357 | private static final int INITIAL_SIZE = 16; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1358 | @UnsupportedAppUsage |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 1359 | public int[] breaks = new int[INITIAL_SIZE]; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1360 | @UnsupportedAppUsage |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 1361 | public float[] widths = new float[INITIAL_SIZE]; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1362 | @UnsupportedAppUsage |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 1363 | public float[] ascents = new float[INITIAL_SIZE]; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1364 | @UnsupportedAppUsage |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 1365 | public float[] descents = new float[INITIAL_SIZE]; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame^] | 1366 | @UnsupportedAppUsage |
Roozbeh Pournader | 112d9c7 | 2015-08-07 12:44:41 -0700 | [diff] [blame] | 1367 | public int[] flags = new int[INITIAL_SIZE]; // hasTab |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 1368 | // breaks, widths, and flags should all have the same length |
| 1369 | } |
| 1370 | |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 1371 | @Nullable private int[] mLeftIndents; |
| 1372 | @Nullable private int[] mRightIndents; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | } |