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