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