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