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 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.graphics.Paint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.text.style.LeadingMarginSpan; |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 22 | import android.text.style.LeadingMarginSpan.LeadingMarginSpan2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.text.style.LineHeightSpan; |
| 24 | import android.text.style.MetricAffectingSpan; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 25 | import android.text.style.TabStopSpan; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 26 | import android.util.Log; |
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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /** |
| 32 | * StaticLayout is a Layout for text that will not be edited after it |
| 33 | * is laid out. Use {@link DynamicLayout} for text that may change. |
| 34 | * <p>This is used by widgets to control text layout. You should not need |
| 35 | * to use this class directly unless you are implementing your own widget |
| 36 | * or custom display object, or would be tempted to call |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 37 | * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, |
| 38 | * float, float, android.graphics.Paint) |
| 39 | * Canvas.drawText()} directly.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | */ |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 41 | public class StaticLayout extends Layout { |
| 42 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 43 | static final String TAG = "StaticLayout"; |
| 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | public StaticLayout(CharSequence source, TextPaint paint, |
| 46 | int width, |
| 47 | Alignment align, float spacingmult, float spacingadd, |
| 48 | boolean includepad) { |
| 49 | this(source, 0, source.length(), paint, width, align, |
| 50 | spacingmult, spacingadd, includepad); |
| 51 | } |
| 52 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 53 | /** |
| 54 | * @hide |
| 55 | */ |
| 56 | public StaticLayout(CharSequence source, TextPaint paint, |
| 57 | int width, Alignment align, TextDirectionHeuristic textDir, |
| 58 | float spacingmult, float spacingadd, |
| 59 | boolean includepad) { |
| 60 | this(source, 0, source.length(), paint, width, align, textDir, |
| 61 | spacingmult, spacingadd, includepad); |
| 62 | } |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 65 | TextPaint paint, int outerwidth, |
| 66 | Alignment align, |
| 67 | float spacingmult, float spacingadd, |
| 68 | boolean includepad) { |
| 69 | this(source, bufstart, bufend, paint, outerwidth, align, |
| 70 | spacingmult, spacingadd, includepad, null, 0); |
| 71 | } |
| 72 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @hide |
| 75 | */ |
| 76 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 77 | TextPaint paint, int outerwidth, |
| 78 | Alignment align, TextDirectionHeuristic textDir, |
| 79 | float spacingmult, float spacingadd, |
| 80 | boolean includepad) { |
| 81 | this(source, bufstart, bufend, paint, outerwidth, align, textDir, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 82 | spacingmult, spacingadd, includepad, null, 0, Integer.MAX_VALUE); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 86 | TextPaint paint, int outerwidth, |
| 87 | Alignment align, |
| 88 | float spacingmult, float spacingadd, |
| 89 | boolean includepad, |
| 90 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 91 | this(source, bufstart, bufend, paint, outerwidth, align, |
| 92 | TextDirectionHeuristics.FIRSTSTRONG_LTR, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 93 | spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @hide |
| 98 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | public StaticLayout(CharSequence source, int bufstart, int bufend, |
| 100 | TextPaint paint, int outerwidth, |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 101 | Alignment align, TextDirectionHeuristic textDir, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | float spacingmult, float spacingadd, |
| 103 | boolean includepad, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 104 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | super((ellipsize == null) |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 106 | ? source |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | : (source instanceof Spanned) |
| 108 | ? new SpannedEllipsizer(source) |
| 109 | : new Ellipsizer(source), |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 110 | paint, outerwidth, align, textDir, spacingmult, spacingadd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * This is annoying, but we can't refer to the layout until |
| 114 | * superclass construction is finished, and the superclass |
| 115 | * constructor wants the reference to the display text. |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 116 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | * This will break if the superclass constructor ever actually |
| 118 | * cares about the content instead of just holding the reference. |
| 119 | */ |
| 120 | if (ellipsize != null) { |
| 121 | Ellipsizer e = (Ellipsizer) getText(); |
| 122 | |
| 123 | e.mLayout = this; |
| 124 | e.mWidth = ellipsizedWidth; |
| 125 | e.mMethod = ellipsize; |
| 126 | mEllipsizedWidth = ellipsizedWidth; |
| 127 | |
| 128 | mColumns = COLUMNS_ELLIPSIZE; |
| 129 | } else { |
| 130 | mColumns = COLUMNS_NORMAL; |
| 131 | mEllipsizedWidth = outerwidth; |
| 132 | } |
| 133 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame^] | 134 | mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns); |
| 135 | mLines = new int[mLineDirections.length]; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 136 | mMaximumVisibleLineCount = maxLines; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 138 | mMeasured = MeasuredText.obtain(); |
| 139 | |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 140 | generate(source, bufstart, bufend, paint, outerwidth, textDir, spacingmult, |
| 141 | spacingadd, includepad, includepad, ellipsizedWidth, |
| 142 | ellipsize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 144 | mMeasured = MeasuredText.recycle(mMeasured); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | mFontMetricsInt = null; |
| 146 | } |
| 147 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 148 | /* package */ StaticLayout(CharSequence text) { |
| 149 | super(text, null, 0, null, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | |
| 151 | mColumns = COLUMNS_ELLIPSIZE; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame^] | 152 | mLineDirections = ArrayUtils.newUnpaddedArray(Directions.class, 2 * mColumns); |
| 153 | mLines = new int[mLineDirections.length]; |
Gilles Debunne | 8154149 | 2012-06-26 16:05:15 -0700 | [diff] [blame] | 154 | // FIXME This is never recycled |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 155 | mMeasured = MeasuredText.obtain(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 158 | /* package */ void generate(CharSequence source, int bufStart, int bufEnd, |
| 159 | TextPaint paint, int outerWidth, |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 160 | TextDirectionHeuristic textDir, float spacingmult, |
| 161 | float spacingadd, boolean includepad, |
| 162 | boolean trackpad, float ellipsizedWidth, |
| 163 | TextUtils.TruncateAt ellipsize) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | mLineCount = 0; |
| 165 | |
| 166 | int v = 0; |
| 167 | boolean needMultiply = (spacingmult != 1 || spacingadd != 0); |
| 168 | |
| 169 | Paint.FontMetricsInt fm = mFontMetricsInt; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 170 | int[] chooseHtv = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 172 | MeasuredText measured = mMeasured; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | Spanned spanned = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | if (source instanceof Spanned) |
| 176 | spanned = (Spanned) source; |
| 177 | |
| 178 | int DEFAULT_DIR = DIR_LEFT_TO_RIGHT; // XXX |
| 179 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 180 | int paraEnd; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 181 | for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) { |
| 182 | paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 183 | if (paraEnd < 0) |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 184 | paraEnd = bufEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | else |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 186 | paraEnd++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 188 | int firstWidthLineLimit = mLineCount + 1; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 189 | int firstWidth = outerWidth; |
| 190 | int restWidth = outerWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 192 | LineHeightSpan[] chooseHt = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
| 194 | if (spanned != null) { |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 195 | LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd, |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 196 | LeadingMarginSpan.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | for (int i = 0; i < sp.length; i++) { |
Mark Wagner | 7b5676e | 2009-10-16 11:44:23 -0700 | [diff] [blame] | 198 | LeadingMarginSpan lms = sp[i]; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 199 | firstWidth -= sp[i].getLeadingMargin(true); |
| 200 | restWidth -= sp[i].getLeadingMargin(false); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 201 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 202 | // LeadingMarginSpan2 is odd. The count affects all |
| 203 | // leading margin spans, not just this particular one, |
| 204 | // and start from the top of the span, not the top of the |
| 205 | // paragraph. |
| 206 | if (lms instanceof LeadingMarginSpan2) { |
| 207 | LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms; |
| 208 | int lmsFirstLine = getLineForOffset(spanned.getSpanStart(lms2)); |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 209 | firstWidthLineLimit = lmsFirstLine + lms2.getLeadingMarginLineCount(); |
Mark Wagner | 7b5676e | 2009-10-16 11:44:23 -0700 | [diff] [blame] | 210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 213 | chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 215 | if (chooseHt.length != 0) { |
| 216 | if (chooseHtv == null || |
| 217 | chooseHtv.length < chooseHt.length) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame^] | 218 | chooseHtv = ArrayUtils.newUnpaddedIntArray(chooseHt.length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 221 | for (int i = 0; i < chooseHt.length; i++) { |
| 222 | int o = spanned.getSpanStart(chooseHt[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 224 | if (o < paraStart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // starts in this layout, before the |
| 226 | // current paragraph |
| 227 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 228 | chooseHtv[i] = getLineTop(getLineForOffset(o)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } else { |
| 230 | // starts in this paragraph |
| 231 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 232 | chooseHtv[i] = v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 238 | measured.setPara(source, paraStart, paraEnd, textDir); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 239 | char[] chs = measured.mChars; |
| 240 | float[] widths = measured.mWidths; |
| 241 | byte[] chdirs = measured.mLevels; |
| 242 | int dir = measured.mDir; |
| 243 | boolean easy = measured.mEasy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 245 | int width = firstWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
| 247 | float w = 0; |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 248 | // here is the offset of the starting character of the line we are currently measuring |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 249 | int here = paraStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 251 | // ok is a character offset located after a word separator (space, tab, number...) where |
| 252 | // we would prefer to cut the current line. Equals to here when no such break was found. |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 253 | int ok = paraStart; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 254 | float okWidth = w; |
| 255 | int okAscent = 0, okDescent = 0, okTop = 0, okBottom = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 257 | // fit is a character offset such that the [here, fit[ range fits in the allowed width. |
| 258 | // We will cut the line there if no ok position is found. |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 259 | int fit = paraStart; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 260 | float fitWidth = w; |
| 261 | int fitAscent = 0, fitDescent = 0, fitTop = 0, fitBottom = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 263 | boolean hasTabOrEmoji = false; |
| 264 | boolean hasTab = false; |
| 265 | TabStops tabStops = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 267 | for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 268 | |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 269 | if (spanned == null) { |
| 270 | spanEnd = paraEnd; |
Doug Felt | 2324188 | 2010-06-02 14:41:06 -0700 | [diff] [blame] | 271 | int spanLen = spanEnd - spanStart; |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 272 | measured.addStyleRun(paint, spanLen, fm); |
| 273 | } else { |
| 274 | spanEnd = spanned.nextSpanTransition(spanStart, paraEnd, |
| 275 | MetricAffectingSpan.class); |
| 276 | int spanLen = spanEnd - spanStart; |
| 277 | MetricAffectingSpan[] spans = |
Doug Felt | 2324188 | 2010-06-02 14:41:06 -0700 | [diff] [blame] | 278 | spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class); |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 279 | spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class); |
| 280 | measured.addStyleRun(paint, spans, spanLen, fm); |
Doug Felt | 2324188 | 2010-06-02 14:41:06 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 283 | int fmTop = fm.top; |
| 284 | int fmBottom = fm.bottom; |
| 285 | int fmAscent = fm.ascent; |
| 286 | int fmDescent = fm.descent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 288 | for (int j = spanStart; j < spanEnd; j++) { |
| 289 | char c = chs[j - paraStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 291 | if (c == CHAR_NEW_LINE) { |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 292 | // intentionally left empty |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 293 | } else if (c == CHAR_TAB) { |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 294 | if (hasTab == false) { |
| 295 | hasTab = true; |
| 296 | hasTabOrEmoji = true; |
Kenny Root | 24ca454 | 2010-06-22 23:46:35 -0700 | [diff] [blame] | 297 | if (spanned != null) { |
| 298 | // First tab this para, check for tabstops |
Eric Fischer | 74d31ef | 2010-08-05 15:29:36 -0700 | [diff] [blame] | 299 | TabStopSpan[] spans = getParagraphSpans(spanned, paraStart, |
Kenny Root | 24ca454 | 2010-06-22 23:46:35 -0700 | [diff] [blame] | 300 | paraEnd, TabStopSpan.class); |
| 301 | if (spans.length > 0) { |
| 302 | tabStops = new TabStops(TAB_INCREMENT, spans); |
| 303 | } |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | if (tabStops != null) { |
| 307 | w = tabStops.nextTab(w); |
| 308 | } else { |
| 309 | w = TabStops.nextDefaultStop(w, TAB_INCREMENT); |
| 310 | } |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 311 | } else if (c >= CHAR_FIRST_HIGH_SURROGATE && c <= CHAR_LAST_LOW_SURROGATE |
| 312 | && j + 1 < spanEnd) { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 313 | int emoji = Character.codePointAt(chs, j - paraStart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 315 | if (emoji >= MIN_EMOJI && emoji <= MAX_EMOJI) { |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 316 | Bitmap bm = EMOJI_FACTORY.getBitmapFromAndroidPua(emoji); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 317 | |
| 318 | if (bm != null) { |
Eric Fischer | 423f0e4 | 2009-03-27 18:04:12 -0700 | [diff] [blame] | 319 | Paint whichPaint; |
| 320 | |
| 321 | if (spanned == null) { |
| 322 | whichPaint = paint; |
| 323 | } else { |
| 324 | whichPaint = mWorkPaint; |
| 325 | } |
| 326 | |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 327 | float wid = bm.getWidth() * -whichPaint.ascent() / bm.getHeight(); |
Eric Fischer | 423f0e4 | 2009-03-27 18:04:12 -0700 | [diff] [blame] | 328 | |
| 329 | w += wid; |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 330 | hasTabOrEmoji = true; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 331 | j++; |
| 332 | } else { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 333 | w += widths[j - paraStart]; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 334 | } |
| 335 | } else { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 336 | w += widths[j - paraStart]; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 337 | } |
| 338 | } else { |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 339 | w += widths[j - paraStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Raph Levien | 8d087c3 | 2013-03-29 09:25:48 -0700 | [diff] [blame] | 342 | boolean isSpaceOrTab = c == CHAR_SPACE || c == CHAR_TAB || c == CHAR_ZWSP; |
Gilles Debunne | 8154149 | 2012-06-26 16:05:15 -0700 | [diff] [blame] | 343 | |
| 344 | if (w <= width || isSpaceOrTab) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 345 | fitWidth = w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | fit = j + 1; |
| 347 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 348 | if (fmTop < fitTop) |
| 349 | fitTop = fmTop; |
| 350 | if (fmAscent < fitAscent) |
| 351 | fitAscent = fmAscent; |
| 352 | if (fmDescent > fitDescent) |
| 353 | fitDescent = fmDescent; |
| 354 | if (fmBottom > fitBottom) |
| 355 | fitBottom = fmBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | |
Gilles Debunne | 8154149 | 2012-06-26 16:05:15 -0700 | [diff] [blame] | 357 | // From the Unicode Line Breaking Algorithm (at least approximately) |
| 358 | boolean isLineBreak = isSpaceOrTab || |
Gilles Debunne | 8154149 | 2012-06-26 16:05:15 -0700 | [diff] [blame] | 359 | // / is class SY and - is class HY, except when followed by a digit |
| 360 | ((c == CHAR_SLASH || c == CHAR_HYPHEN) && |
| 361 | (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) || |
| 362 | // Ideographs are class ID: breakpoints when adjacent, except for NS |
| 363 | // (non-starters), which can be broken after but not before |
| 364 | (c >= CHAR_FIRST_CJK && isIdeographic(c, true) && |
| 365 | j + 1 < spanEnd && isIdeographic(chs[j + 1 - paraStart], false)); |
| 366 | |
| 367 | if (isLineBreak) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 368 | okWidth = w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | ok = j + 1; |
| 370 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 371 | if (fitTop < okTop) |
| 372 | okTop = fitTop; |
| 373 | if (fitAscent < okAscent) |
| 374 | okAscent = fitAscent; |
| 375 | if (fitDescent > okDescent) |
| 376 | okDescent = fitDescent; |
| 377 | if (fitBottom > okBottom) |
| 378 | okBottom = fitBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
Gilles Debunne | 4cf435d | 2011-01-04 15:35:29 -0800 | [diff] [blame] | 380 | } else { |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 381 | final boolean moreChars = (j + 1 < spanEnd); |
| 382 | int endPos; |
| 383 | int above, below, top, bottom; |
| 384 | float currentTextWidth; |
Gilles Debunne | 32ea4ff | 2010-12-21 11:28:34 -0800 | [diff] [blame] | 385 | |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 386 | if (ok != here) { |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 387 | endPos = ok; |
| 388 | above = okAscent; |
| 389 | below = okDescent; |
| 390 | top = okTop; |
| 391 | bottom = okBottom; |
| 392 | currentTextWidth = okWidth; |
| 393 | } else if (fit != here) { |
| 394 | endPos = fit; |
| 395 | above = fitAscent; |
| 396 | below = fitDescent; |
| 397 | top = fitTop; |
| 398 | bottom = fitBottom; |
| 399 | currentTextWidth = fitWidth; |
| 400 | } else { |
| 401 | endPos = here + 1; |
| 402 | above = fm.ascent; |
| 403 | below = fm.descent; |
| 404 | top = fm.top; |
| 405 | bottom = fm.bottom; |
| 406 | currentTextWidth = widths[here - paraStart]; |
| 407 | } |
| 408 | |
| 409 | v = out(source, here, endPos, |
| 410 | above, below, top, bottom, |
| 411 | v, spacingmult, spacingadd, chooseHt,chooseHtv, fm, hasTabOrEmoji, |
| 412 | needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad, |
| 413 | chs, widths, paraStart, ellipsize, ellipsizedWidth, |
| 414 | currentTextWidth, paint, moreChars); |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 415 | |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 416 | here = endPos; |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 417 | j = here - 1; // restart j-span loop from here, compensating for the j++ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | ok = fit = here; |
| 419 | w = 0; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 420 | fitAscent = fitDescent = fitTop = fitBottom = 0; |
| 421 | okAscent = okDescent = okTop = okBottom = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 423 | if (--firstWidthLineLimit <= 0) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 424 | width = restWidth; |
Mark Wagner | 7b5676e | 2009-10-16 11:44:23 -0700 | [diff] [blame] | 425 | } |
Gilles Debunne | cd943a7 | 2012-06-07 17:54:47 -0700 | [diff] [blame] | 426 | |
| 427 | if (here < spanStart) { |
| 428 | // The text was cut before the beginning of the current span range. |
| 429 | // Exit the span loop, and get spanStart to start over from here. |
| 430 | measured.setPos(here); |
| 431 | spanEnd = here; |
| 432 | break; |
| 433 | } |
Gilles Debunne | 8154149 | 2012-06-26 16:05:15 -0700 | [diff] [blame] | 434 | |
| 435 | if (mLineCount >= mMaximumVisibleLineCount) { |
| 436 | break; |
| 437 | } |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Fabrice Di Meglio | ad0b051 | 2011-10-04 17:21:26 -0700 | [diff] [blame] | 442 | if (paraEnd != here && mLineCount < mMaximumVisibleLineCount) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 443 | if ((fitTop | fitBottom | fitDescent | fitAscent) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | paint.getFontMetricsInt(fm); |
| 445 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 446 | fitTop = fm.top; |
| 447 | fitBottom = fm.bottom; |
| 448 | fitAscent = fm.ascent; |
| 449 | fitDescent = fm.descent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Log.e("text", "output rest " + here + " to " + end); |
| 453 | |
| 454 | v = out(source, |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 455 | here, paraEnd, fitAscent, fitDescent, |
| 456 | fitTop, fitBottom, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | v, |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 458 | spacingmult, spacingadd, chooseHt, |
| 459 | chooseHtv, fm, hasTabOrEmoji, |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 460 | needMultiply, chdirs, dir, easy, bufEnd, |
| 461 | includepad, trackpad, chs, |
| 462 | widths, paraStart, ellipsize, |
| 463 | ellipsizedWidth, w, paint, paraEnd != bufEnd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 466 | paraStart = paraEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 468 | if (paraEnd == bufEnd) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | break; |
| 470 | } |
| 471 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 472 | if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) && |
Fabrice Di Meglio | ad0b051 | 2011-10-04 17:21:26 -0700 | [diff] [blame] | 473 | mLineCount < mMaximumVisibleLineCount) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 474 | // Log.e("text", "output last " + bufEnd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | |
Fabrice Di Meglio | e631889 | 2013-06-18 20:03:41 -0700 | [diff] [blame] | 476 | measured.setPara(source, bufStart, bufEnd, textDir); |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | paint.getFontMetricsInt(fm); |
| 479 | |
| 480 | v = out(source, |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 481 | bufEnd, bufEnd, fm.ascent, fm.descent, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | fm.top, fm.bottom, |
| 483 | v, |
| 484 | spacingmult, spacingadd, null, |
| 485 | null, fm, false, |
Fabrice Di Meglio | e631889 | 2013-06-18 20:03:41 -0700 | [diff] [blame] | 486 | needMultiply, measured.mLevels, measured.mDir, measured.mEasy, bufEnd, |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 487 | includepad, trackpad, null, |
| 488 | null, bufStart, ellipsize, |
| 489 | ellipsizedWidth, 0, paint, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | /** |
| 494 | * Returns true if the specified character is one of those specified |
| 495 | * as being Ideographic (class ID) by the Unicode Line Breaking Algorithm |
| 496 | * (http://www.unicode.org/unicode/reports/tr14/), and is therefore OK |
| 497 | * to break between a pair of. |
Eric Fischer | 549d724 | 2009-03-31 14:19:47 -0700 | [diff] [blame] | 498 | * |
| 499 | * @param includeNonStarters also return true for category NS |
| 500 | * (non-starters), which can be broken |
| 501 | * after but not before. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | */ |
Eric Fischer | 549d724 | 2009-03-31 14:19:47 -0700 | [diff] [blame] | 503 | private static final boolean isIdeographic(char c, boolean includeNonStarters) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | if (c >= '\u2E80' && c <= '\u2FFF') { |
| 505 | return true; // CJK, KANGXI RADICALS, DESCRIPTION SYMBOLS |
| 506 | } |
| 507 | if (c == '\u3000') { |
| 508 | return true; // IDEOGRAPHIC SPACE |
| 509 | } |
| 510 | if (c >= '\u3040' && c <= '\u309F') { |
Eric Fischer | 549d724 | 2009-03-31 14:19:47 -0700 | [diff] [blame] | 511 | if (!includeNonStarters) { |
| 512 | switch (c) { |
| 513 | case '\u3041': // # HIRAGANA LETTER SMALL A |
| 514 | case '\u3043': // # HIRAGANA LETTER SMALL I |
| 515 | case '\u3045': // # HIRAGANA LETTER SMALL U |
| 516 | case '\u3047': // # HIRAGANA LETTER SMALL E |
| 517 | case '\u3049': // # HIRAGANA LETTER SMALL O |
| 518 | case '\u3063': // # HIRAGANA LETTER SMALL TU |
| 519 | case '\u3083': // # HIRAGANA LETTER SMALL YA |
| 520 | case '\u3085': // # HIRAGANA LETTER SMALL YU |
| 521 | case '\u3087': // # HIRAGANA LETTER SMALL YO |
| 522 | case '\u308E': // # HIRAGANA LETTER SMALL WA |
| 523 | case '\u3095': // # HIRAGANA LETTER SMALL KA |
| 524 | case '\u3096': // # HIRAGANA LETTER SMALL KE |
| 525 | case '\u309B': // # KATAKANA-HIRAGANA VOICED SOUND MARK |
| 526 | case '\u309C': // # KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK |
| 527 | case '\u309D': // # HIRAGANA ITERATION MARK |
| 528 | case '\u309E': // # HIRAGANA VOICED ITERATION MARK |
| 529 | return false; |
| 530 | } |
| 531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | return true; // Hiragana (except small characters) |
| 533 | } |
| 534 | if (c >= '\u30A0' && c <= '\u30FF') { |
Eric Fischer | 549d724 | 2009-03-31 14:19:47 -0700 | [diff] [blame] | 535 | if (!includeNonStarters) { |
| 536 | switch (c) { |
| 537 | case '\u30A0': // # KATAKANA-HIRAGANA DOUBLE HYPHEN |
| 538 | case '\u30A1': // # KATAKANA LETTER SMALL A |
| 539 | case '\u30A3': // # KATAKANA LETTER SMALL I |
| 540 | case '\u30A5': // # KATAKANA LETTER SMALL U |
| 541 | case '\u30A7': // # KATAKANA LETTER SMALL E |
| 542 | case '\u30A9': // # KATAKANA LETTER SMALL O |
| 543 | case '\u30C3': // # KATAKANA LETTER SMALL TU |
| 544 | case '\u30E3': // # KATAKANA LETTER SMALL YA |
| 545 | case '\u30E5': // # KATAKANA LETTER SMALL YU |
| 546 | case '\u30E7': // # KATAKANA LETTER SMALL YO |
| 547 | case '\u30EE': // # KATAKANA LETTER SMALL WA |
| 548 | case '\u30F5': // # KATAKANA LETTER SMALL KA |
| 549 | case '\u30F6': // # KATAKANA LETTER SMALL KE |
| 550 | case '\u30FB': // # KATAKANA MIDDLE DOT |
| 551 | case '\u30FC': // # KATAKANA-HIRAGANA PROLONGED SOUND MARK |
| 552 | case '\u30FD': // # KATAKANA ITERATION MARK |
| 553 | case '\u30FE': // # KATAKANA VOICED ITERATION MARK |
| 554 | return false; |
| 555 | } |
| 556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | return true; // Katakana (except small characters) |
| 558 | } |
| 559 | if (c >= '\u3400' && c <= '\u4DB5') { |
| 560 | return true; // CJK UNIFIED IDEOGRAPHS EXTENSION A |
| 561 | } |
| 562 | if (c >= '\u4E00' && c <= '\u9FBB') { |
| 563 | return true; // CJK UNIFIED IDEOGRAPHS |
| 564 | } |
| 565 | if (c >= '\uF900' && c <= '\uFAD9') { |
| 566 | return true; // CJK COMPATIBILITY IDEOGRAPHS |
| 567 | } |
| 568 | if (c >= '\uA000' && c <= '\uA48F') { |
| 569 | return true; // YI SYLLABLES |
| 570 | } |
| 571 | if (c >= '\uA490' && c <= '\uA4CF') { |
| 572 | return true; // YI RADICALS |
| 573 | } |
| 574 | if (c >= '\uFE62' && c <= '\uFE66') { |
| 575 | return true; // SMALL PLUS SIGN to SMALL EQUALS SIGN |
| 576 | } |
| 577 | if (c >= '\uFF10' && c <= '\uFF19') { |
| 578 | return true; // WIDE DIGITS |
| 579 | } |
| 580 | |
| 581 | return false; |
| 582 | } |
| 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | private int out(CharSequence text, int start, int end, |
| 585 | int above, int below, int top, int bottom, int v, |
| 586 | float spacingmult, float spacingadd, |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 587 | LineHeightSpan[] chooseHt, int[] chooseHtv, |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 588 | Paint.FontMetricsInt fm, boolean hasTabOrEmoji, |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 589 | boolean needMultiply, byte[] chdirs, int dir, |
| 590 | boolean easy, int bufEnd, boolean includePad, |
| 591 | boolean trackPad, char[] chs, |
| 592 | float[] widths, int widthStart, TextUtils.TruncateAt ellipsize, |
| 593 | float ellipsisWidth, float textWidth, |
| 594 | TextPaint paint, boolean moreChars) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | int j = mLineCount; |
| 596 | int off = j * mColumns; |
| 597 | int want = off + mColumns + TOP; |
| 598 | int[] lines = mLines; |
| 599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | if (want >= lines.length) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame^] | 601 | Directions[] grow2 = ArrayUtils.newUnpaddedArray( |
| 602 | Directions.class, GrowingArrayUtils.growSize(want)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | System.arraycopy(mLineDirections, 0, grow2, 0, |
| 604 | mLineDirections.length); |
| 605 | mLineDirections = grow2; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame^] | 606 | |
| 607 | int[] grow = new int[grow2.length]; |
| 608 | System.arraycopy(lines, 0, grow, 0, lines.length); |
| 609 | mLines = grow; |
| 610 | lines = grow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 613 | if (chooseHt != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | fm.ascent = above; |
| 615 | fm.descent = below; |
| 616 | fm.top = top; |
| 617 | fm.bottom = bottom; |
| 618 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 619 | for (int i = 0; i < chooseHt.length; i++) { |
| 620 | if (chooseHt[i] instanceof LineHeightSpan.WithDensity) { |
| 621 | ((LineHeightSpan.WithDensity) chooseHt[i]). |
| 622 | chooseHeight(text, start, end, chooseHtv[i], v, fm, paint); |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 623 | |
| 624 | } else { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 625 | chooseHt[i].chooseHeight(text, start, end, chooseHtv[i], v, fm); |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | above = fm.ascent; |
| 630 | below = fm.descent; |
| 631 | top = fm.top; |
| 632 | bottom = fm.bottom; |
| 633 | } |
| 634 | |
| 635 | if (j == 0) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 636 | if (trackPad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | mTopPadding = top - above; |
| 638 | } |
| 639 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 640 | if (includePad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | above = top; |
| 642 | } |
| 643 | } |
Gilles Debunne | d300e75 | 2011-10-17 13:37:36 -0700 | [diff] [blame] | 644 | if (end == bufEnd) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 645 | if (trackPad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | mBottomPadding = bottom - below; |
| 647 | } |
| 648 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 649 | if (includePad) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | below = bottom; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | int extra; |
| 655 | |
| 656 | if (needMultiply) { |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 657 | double ex = (below - above) * (spacingmult - 1) + spacingadd; |
| 658 | if (ex >= 0) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 659 | extra = (int)(ex + EXTRA_ROUNDING); |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 660 | } else { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 661 | extra = -(int)(-ex + EXTRA_ROUNDING); |
Doug Felt | 1065758 | 2010-02-22 11:19:01 -0800 | [diff] [blame] | 662 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | } else { |
| 664 | extra = 0; |
| 665 | } |
| 666 | |
| 667 | lines[off + START] = start; |
| 668 | lines[off + TOP] = v; |
| 669 | lines[off + DESCENT] = below + extra; |
| 670 | |
| 671 | v += (below - above) + extra; |
| 672 | lines[off + mColumns + START] = end; |
| 673 | lines[off + mColumns + TOP] = v; |
| 674 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 675 | if (hasTabOrEmoji) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | lines[off + TAB] |= TAB_MASK; |
| 677 | |
Doug Felt | 9f7a444 | 2010-03-01 12:45:56 -0800 | [diff] [blame] | 678 | lines[off + DIR] |= dir << DIR_SHIFT; |
| 679 | Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT; |
| 680 | // easy means all chars < the first RTL, so no emoji, no nothing |
Doug Felt | 4e0c5e5 | 2010-03-15 16:56:02 -0700 | [diff] [blame] | 681 | // 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] | 682 | // RTL paragraph. Make sure easy is false if this is the case. |
| 683 | if (easy) { |
| 684 | mLineDirections[j] = linedirs; |
| 685 | } else { |
Gilles Debunne | f3fa0cd | 2011-02-03 14:17:05 -0800 | [diff] [blame] | 686 | mLineDirections[j] = AndroidBidi.directions(dir, chdirs, start - widthStart, chs, |
| 687 | start - widthStart, end - start); |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | |
Fabrice Di Meglio | aef455f | 2011-08-29 15:39:11 -0700 | [diff] [blame] | 690 | if (ellipsize != null) { |
| 691 | // If there is only one line, then do any type of ellipsis except when it is MARQUEE |
| 692 | // if there are multiple lines, just allow END ellipsis on the last line |
| 693 | boolean firstLine = (j == 0); |
| 694 | boolean currentLineIsTheLastVisibleOne = (j + 1 == mMaximumVisibleLineCount); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 695 | boolean forceEllipsis = moreChars && (mLineCount + 1 == mMaximumVisibleLineCount); |
Fabrice Di Meglio | aef455f | 2011-08-29 15:39:11 -0700 | [diff] [blame] | 696 | |
Fabrice Di Meglio | 34a126e | 2012-02-29 18:43:14 -0800 | [diff] [blame] | 697 | boolean doEllipsis = |
| 698 | (((mMaximumVisibleLineCount == 1 && moreChars) || (firstLine && !moreChars)) && |
Fabrice Di Meglio | aef455f | 2011-08-29 15:39:11 -0700 | [diff] [blame] | 699 | ellipsize != TextUtils.TruncateAt.MARQUEE) || |
| 700 | (!firstLine && (currentLineIsTheLastVisibleOne || !moreChars) && |
| 701 | ellipsize == TextUtils.TruncateAt.END); |
| 702 | if (doEllipsis) { |
| 703 | calculateEllipsis(start, end, widths, widthStart, |
| 704 | ellipsisWidth, ellipsize, j, |
| 705 | textWidth, paint, forceEllipsis); |
| 706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | mLineCount++; |
| 710 | return v; |
| 711 | } |
| 712 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 713 | private void calculateEllipsis(int lineStart, int lineEnd, |
| 714 | float[] widths, int widthStart, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | float avail, TextUtils.TruncateAt where, |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 716 | int line, float textWidth, TextPaint paint, |
| 717 | boolean forceEllipsis) { |
| 718 | if (textWidth <= avail && !forceEllipsis) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | // Everything fits! |
| 720 | mLines[mColumns * line + ELLIPSIS_START] = 0; |
| 721 | mLines[mColumns * line + ELLIPSIS_COUNT] = 0; |
| 722 | return; |
| 723 | } |
| 724 | |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 725 | float ellipsisWidth = paint.measureText( |
Fabrice Di Meglio | 8d44fff | 2012-06-13 15:45:38 -0700 | [diff] [blame] | 726 | (where == TextUtils.TruncateAt.END_SMALL) ? |
| 727 | ELLIPSIS_TWO_DOTS : ELLIPSIS_NORMAL, 0, 1); |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 728 | int ellipsisStart = 0; |
| 729 | int ellipsisCount = 0; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 730 | int len = lineEnd - lineStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 732 | // We only support start ellipsis on a single line |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | if (where == TextUtils.TruncateAt.START) { |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 734 | if (mMaximumVisibleLineCount == 1) { |
| 735 | float sum = 0; |
| 736 | int i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 738 | for (i = len; i >= 0; i--) { |
| 739 | float w = widths[i - 1 + lineStart - widthStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 741 | if (w + sum + ellipsisWidth > avail) { |
| 742 | break; |
| 743 | } |
| 744 | |
| 745 | sum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 748 | ellipsisStart = 0; |
| 749 | ellipsisCount = i; |
| 750 | } else { |
| 751 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 752 | Log.w(TAG, "Start Ellipsis only supported with one line"); |
| 753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | } |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 755 | } else if (where == TextUtils.TruncateAt.END || where == TextUtils.TruncateAt.MARQUEE || |
| 756 | where == TextUtils.TruncateAt.END_SMALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | float sum = 0; |
| 758 | int i; |
| 759 | |
| 760 | for (i = 0; i < len; i++) { |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 761 | float w = widths[i + lineStart - widthStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 763 | if (w + sum + ellipsisWidth > avail) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | |
| 767 | sum += w; |
| 768 | } |
| 769 | |
| 770 | ellipsisStart = i; |
| 771 | ellipsisCount = len - i; |
Fabrice Di Meglio | aef455f | 2011-08-29 15:39:11 -0700 | [diff] [blame] | 772 | if (forceEllipsis && ellipsisCount == 0 && len > 0) { |
| 773 | ellipsisStart = len - 1; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 774 | ellipsisCount = 1; |
| 775 | } |
| 776 | } else { |
| 777 | // where = TextUtils.TruncateAt.MIDDLE We only support middle ellipsis on a single line |
| 778 | if (mMaximumVisibleLineCount == 1) { |
| 779 | float lsum = 0, rsum = 0; |
| 780 | int left = 0, right = len; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 782 | float ravail = (avail - ellipsisWidth) / 2; |
| 783 | for (right = len; right >= 0; right--) { |
| 784 | float w = widths[right - 1 + lineStart - widthStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 786 | if (w + rsum > ravail) { |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | rsum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 793 | float lavail = avail - ellipsisWidth - rsum; |
| 794 | for (left = 0; left < right; left++) { |
| 795 | float w = widths[left + lineStart - widthStart]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 797 | if (w + lsum > lavail) { |
| 798 | break; |
| 799 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 801 | lsum += w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 804 | ellipsisStart = left; |
| 805 | ellipsisCount = right - left; |
| 806 | } else { |
| 807 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 808 | Log.w(TAG, "Middle Ellipsis only supported with one line"); |
| 809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | mLines[mColumns * line + ELLIPSIS_START] = ellipsisStart; |
| 814 | mLines[mColumns * line + ELLIPSIS_COUNT] = ellipsisCount; |
| 815 | } |
| 816 | |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 817 | // 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] | 818 | // rather than relying on member functions. |
| 819 | // The logic mirrors that of Layout.getLineForVertical |
| 820 | // 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] | 821 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | public int getLineForVertical(int vertical) { |
| 823 | int high = mLineCount; |
| 824 | int low = -1; |
| 825 | int guess; |
| 826 | int[] lines = mLines; |
| 827 | while (high - low > 1) { |
| 828 | guess = (high + low) >> 1; |
| 829 | if (lines[mColumns * guess + TOP] > vertical){ |
| 830 | high = guess; |
| 831 | } else { |
| 832 | low = guess; |
| 833 | } |
| 834 | } |
| 835 | if (low < 0) { |
| 836 | return 0; |
| 837 | } else { |
| 838 | return low; |
| 839 | } |
| 840 | } |
| 841 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 842 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | public int getLineCount() { |
| 844 | return mLineCount; |
| 845 | } |
| 846 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 847 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | public int getLineTop(int line) { |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 849 | int top = mLines[mColumns * line + TOP]; |
| 850 | if (mMaximumVisibleLineCount > 0 && line >= mMaximumVisibleLineCount && |
| 851 | line != mLineCount) { |
| 852 | top += getBottomPadding(); |
| 853 | } |
| 854 | return top; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 857 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | public int getLineDescent(int line) { |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 859 | int descent = mLines[mColumns * line + DESCENT]; |
Gilles Debunne | f3fa0cd | 2011-02-03 14:17:05 -0800 | [diff] [blame] | 860 | if (mMaximumVisibleLineCount > 0 && line >= mMaximumVisibleLineCount - 1 && // -1 intended |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 861 | line != mLineCount) { |
| 862 | descent += getBottomPadding(); |
| 863 | } |
| 864 | return descent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 867 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | public int getLineStart(int line) { |
| 869 | return mLines[mColumns * line + START] & START_MASK; |
| 870 | } |
| 871 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 872 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | public int getParagraphDirection(int line) { |
| 874 | return mLines[mColumns * line + DIR] >> DIR_SHIFT; |
| 875 | } |
| 876 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 877 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | public boolean getLineContainsTab(int line) { |
| 879 | return (mLines[mColumns * line + TAB] & TAB_MASK) != 0; |
| 880 | } |
| 881 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 882 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | public final Directions getLineDirections(int line) { |
| 884 | return mLineDirections[line]; |
| 885 | } |
| 886 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 887 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | public int getTopPadding() { |
| 889 | return mTopPadding; |
| 890 | } |
| 891 | |
Gilles Debunne | 6611147 | 2010-11-19 11:04:37 -0800 | [diff] [blame] | 892 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | public int getBottomPadding() { |
| 894 | return mBottomPadding; |
| 895 | } |
| 896 | |
| 897 | @Override |
| 898 | public int getEllipsisCount(int line) { |
| 899 | if (mColumns < COLUMNS_ELLIPSIZE) { |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | return mLines[mColumns * line + ELLIPSIS_COUNT]; |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | public int getEllipsisStart(int line) { |
| 908 | if (mColumns < COLUMNS_ELLIPSIZE) { |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | return mLines[mColumns * line + ELLIPSIS_START]; |
| 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public int getEllipsizedWidth() { |
| 917 | return mEllipsizedWidth; |
| 918 | } |
| 919 | |
Romain Guy | e5ea440 | 2011-08-01 14:01:37 -0700 | [diff] [blame] | 920 | void prepare() { |
| 921 | mMeasured = MeasuredText.obtain(); |
| 922 | } |
| 923 | |
| 924 | void finish() { |
| 925 | mMeasured = MeasuredText.recycle(mMeasured); |
| 926 | } |
Gilles Debunne | 0a4db3c | 2011-01-14 12:12:04 -0800 | [diff] [blame] | 927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | private int mLineCount; |
| 929 | private int mTopPadding, mBottomPadding; |
| 930 | private int mColumns; |
| 931 | private int mEllipsizedWidth; |
| 932 | |
| 933 | private static final int COLUMNS_NORMAL = 3; |
| 934 | private static final int COLUMNS_ELLIPSIZE = 5; |
| 935 | private static final int START = 0; |
| 936 | private static final int DIR = START; |
| 937 | private static final int TAB = START; |
| 938 | private static final int TOP = 1; |
| 939 | private static final int DESCENT = 2; |
| 940 | private static final int ELLIPSIS_START = 3; |
| 941 | private static final int ELLIPSIS_COUNT = 4; |
| 942 | |
| 943 | private int[] mLines; |
| 944 | private Directions[] mLineDirections; |
Fabrice Di Meglio | 8059e090 | 2011-08-10 16:31:58 -0700 | [diff] [blame] | 945 | private int mMaximumVisibleLineCount = Integer.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | |
| 947 | private static final int START_MASK = 0x1FFFFFFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | private static final int DIR_SHIFT = 30; |
| 949 | private static final int TAB_MASK = 0x20000000; |
| 950 | |
Doug Felt | c982f60 | 2010-05-25 11:51:40 -0700 | [diff] [blame] | 951 | 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] | 952 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 953 | private static final char CHAR_FIRST_CJK = '\u2E80'; |
| 954 | |
| 955 | private static final char CHAR_NEW_LINE = '\n'; |
| 956 | private static final char CHAR_TAB = '\t'; |
| 957 | private static final char CHAR_SPACE = ' '; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 958 | private static final char CHAR_SLASH = '/'; |
| 959 | private static final char CHAR_HYPHEN = '-'; |
Raph Levien | 8d087c3 | 2013-03-29 09:25:48 -0700 | [diff] [blame] | 960 | private static final char CHAR_ZWSP = '\u200B'; |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 961 | |
| 962 | private static final double EXTRA_ROUNDING = 0.5; |
Fabrice Di Meglio | cb33264 | 2011-09-23 19:08:04 -0700 | [diff] [blame] | 963 | |
Fabrice Di Meglio | 121c82c | 2011-02-15 15:44:49 -0800 | [diff] [blame] | 964 | private static final int CHAR_FIRST_HIGH_SURROGATE = 0xD800; |
| 965 | private static final int CHAR_LAST_LOW_SURROGATE = 0xDFFF; |
| 966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | /* |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 968 | * This is reused across calls to generate() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | */ |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 970 | private MeasuredText mMeasured; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | private Paint.FontMetricsInt mFontMetricsInt = new Paint.FontMetricsInt(); |
| 972 | } |