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