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