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