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