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