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