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