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.Canvas; |
| 20 | import android.graphics.Paint; |
| 21 | import android.graphics.Path; |
Eric Fischer | 86fcef8 | 2009-08-17 17:16:44 -0700 | [diff] [blame] | 22 | import android.text.style.ParagraphStyle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * A BoringLayout is a very simple Layout implementation for text that |
| 26 | * fits on a single line and is all left-to-right characters. |
| 27 | * You will probably never want to make one of these yourself; |
| 28 | * if you do, be sure to call {@link #isBoring} first to make sure |
| 29 | * the text meets the criteria. |
| 30 | * <p>This class is used by widgets to control text layout. You should not need |
| 31 | * to use this class directly unless you are implementing your own widget |
| 32 | * or custom display object, in which case |
| 33 | * you are encouraged to use a Layout instead of calling |
| 34 | * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, float, float, android.graphics.Paint) |
| 35 | * Canvas.drawText()} directly.</p> |
| 36 | */ |
| 37 | public class BoringLayout extends Layout implements TextUtils.EllipsizeCallback { |
| 38 | public static BoringLayout make(CharSequence source, |
| 39 | TextPaint paint, int outerwidth, |
| 40 | Alignment align, |
| 41 | float spacingmult, float spacingadd, |
| 42 | BoringLayout.Metrics metrics, boolean includepad) { |
| 43 | return new BoringLayout(source, paint, outerwidth, align, |
| 44 | spacingmult, spacingadd, metrics, |
| 45 | includepad); |
| 46 | } |
| 47 | |
| 48 | public static BoringLayout make(CharSequence source, |
| 49 | TextPaint paint, int outerwidth, |
| 50 | Alignment align, |
| 51 | float spacingmult, float spacingadd, |
| 52 | BoringLayout.Metrics metrics, boolean includepad, |
| 53 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 54 | return new BoringLayout(source, paint, outerwidth, align, |
| 55 | spacingmult, spacingadd, metrics, |
| 56 | includepad, ellipsize, ellipsizedWidth); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns a BoringLayout for the specified text, potentially reusing |
| 61 | * this one if it is already suitable. The caller must make sure that |
| 62 | * no one is still using this Layout. |
| 63 | */ |
| 64 | public BoringLayout replaceOrMake(CharSequence source, TextPaint paint, |
| 65 | int outerwidth, Alignment align, |
| 66 | float spacingmult, float spacingadd, |
| 67 | BoringLayout.Metrics metrics, |
| 68 | boolean includepad) { |
| 69 | replaceWith(source, paint, outerwidth, align, spacingmult, |
| 70 | spacingadd); |
| 71 | |
| 72 | mEllipsizedWidth = outerwidth; |
| 73 | mEllipsizedStart = 0; |
| 74 | mEllipsizedCount = 0; |
| 75 | |
| 76 | init(source, paint, outerwidth, align, spacingmult, spacingadd, |
| 77 | metrics, includepad, true); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns a BoringLayout for the specified text, potentially reusing |
| 83 | * this one if it is already suitable. The caller must make sure that |
| 84 | * no one is still using this Layout. |
| 85 | */ |
| 86 | public BoringLayout replaceOrMake(CharSequence source, TextPaint paint, |
| 87 | int outerwidth, Alignment align, |
| 88 | float spacingmult, float spacingadd, |
| 89 | BoringLayout.Metrics metrics, |
| 90 | boolean includepad, |
| 91 | TextUtils.TruncateAt ellipsize, |
| 92 | int ellipsizedWidth) { |
| 93 | boolean trust; |
| 94 | |
| 95 | if (ellipsize == null || ellipsize == TextUtils.TruncateAt.MARQUEE) { |
| 96 | replaceWith(source, paint, outerwidth, align, spacingmult, |
| 97 | spacingadd); |
| 98 | |
| 99 | mEllipsizedWidth = outerwidth; |
| 100 | mEllipsizedStart = 0; |
| 101 | mEllipsizedCount = 0; |
| 102 | trust = true; |
| 103 | } else { |
| 104 | replaceWith(TextUtils.ellipsize(source, paint, ellipsizedWidth, |
| 105 | ellipsize, true, this), |
| 106 | paint, outerwidth, align, spacingmult, |
| 107 | spacingadd); |
| 108 | |
| 109 | mEllipsizedWidth = ellipsizedWidth; |
| 110 | trust = false; |
| 111 | } |
| 112 | |
| 113 | init(getText(), paint, outerwidth, align, spacingmult, spacingadd, |
| 114 | metrics, includepad, trust); |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | public BoringLayout(CharSequence source, |
| 119 | TextPaint paint, int outerwidth, |
| 120 | Alignment align, |
| 121 | float spacingmult, float spacingadd, |
| 122 | BoringLayout.Metrics metrics, boolean includepad) { |
| 123 | super(source, paint, outerwidth, align, spacingmult, spacingadd); |
| 124 | |
| 125 | mEllipsizedWidth = outerwidth; |
| 126 | mEllipsizedStart = 0; |
| 127 | mEllipsizedCount = 0; |
| 128 | |
| 129 | init(source, paint, outerwidth, align, spacingmult, spacingadd, |
| 130 | metrics, includepad, true); |
| 131 | } |
| 132 | |
| 133 | public BoringLayout(CharSequence source, |
| 134 | TextPaint paint, int outerwidth, |
| 135 | Alignment align, |
| 136 | float spacingmult, float spacingadd, |
| 137 | BoringLayout.Metrics metrics, boolean includepad, |
| 138 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 139 | /* |
| 140 | * It is silly to have to call super() and then replaceWith(), |
| 141 | * but we can't use "this" for the callback until the call to |
| 142 | * super() finishes. |
| 143 | */ |
| 144 | super(source, paint, outerwidth, align, spacingmult, spacingadd); |
| 145 | |
| 146 | boolean trust; |
| 147 | |
| 148 | if (ellipsize == null || ellipsize == TextUtils.TruncateAt.MARQUEE) { |
| 149 | mEllipsizedWidth = outerwidth; |
| 150 | mEllipsizedStart = 0; |
| 151 | mEllipsizedCount = 0; |
| 152 | trust = true; |
| 153 | } else { |
| 154 | replaceWith(TextUtils.ellipsize(source, paint, ellipsizedWidth, |
| 155 | ellipsize, true, this), |
| 156 | paint, outerwidth, align, spacingmult, |
| 157 | spacingadd); |
| 158 | |
| 159 | |
| 160 | mEllipsizedWidth = ellipsizedWidth; |
| 161 | trust = false; |
| 162 | } |
| 163 | |
| 164 | init(getText(), paint, outerwidth, align, spacingmult, spacingadd, |
| 165 | metrics, includepad, trust); |
| 166 | } |
| 167 | |
| 168 | /* package */ void init(CharSequence source, |
| 169 | TextPaint paint, int outerwidth, |
| 170 | Alignment align, |
| 171 | float spacingmult, float spacingadd, |
| 172 | BoringLayout.Metrics metrics, boolean includepad, |
| 173 | boolean trustWidth) { |
| 174 | int spacing; |
| 175 | |
| 176 | if (source instanceof String && align == Layout.Alignment.ALIGN_NORMAL) { |
| 177 | mDirect = source.toString(); |
| 178 | } else { |
| 179 | mDirect = null; |
| 180 | } |
| 181 | |
| 182 | mPaint = paint; |
| 183 | |
| 184 | if (includepad) { |
| 185 | spacing = metrics.bottom - metrics.top; |
Raph Levien | 07e6c23 | 2016-04-04 12:34:06 -0700 | [diff] [blame] | 186 | mDesc = metrics.bottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } else { |
| 188 | spacing = metrics.descent - metrics.ascent; |
Raph Levien | 07e6c23 | 2016-04-04 12:34:06 -0700 | [diff] [blame] | 189 | mDesc = metrics.descent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | mBottom = spacing; |
| 193 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | if (trustWidth) { |
| 195 | mMax = metrics.width; |
| 196 | } else { |
| 197 | /* |
| 198 | * If we have ellipsized, we have to actually calculate the |
| 199 | * width because the width that was passed in was for the |
| 200 | * full text, not the ellipsized form. |
| 201 | */ |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 202 | TextLine line = TextLine.obtain(); |
| 203 | line.set(paint, source, 0, source.length(), Layout.DIR_LEFT_TO_RIGHT, |
| 204 | Layout.DIRS_ALL_LEFT_TO_RIGHT, false, null); |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 205 | mMax = (int) Math.ceil(line.metrics(null)); |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 206 | TextLine.recycle(line); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (includepad) { |
| 210 | mTopPadding = metrics.top - metrics.ascent; |
| 211 | mBottomPadding = metrics.bottom - metrics.descent; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Returns null if not boring; the width, ascent, and descent if boring. |
| 217 | */ |
| 218 | public static Metrics isBoring(CharSequence text, |
| 219 | TextPaint paint) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 220 | return isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, null); |
| 221 | } |
| 222 | |
| 223 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | * Returns null if not boring; the width, ascent, and descent in the |
| 225 | * provided Metrics object (or a new one if the provided one was null) |
| 226 | * if boring. |
| 227 | */ |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 228 | public static Metrics isBoring(CharSequence text, TextPaint paint, Metrics metrics) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 229 | return isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, metrics); |
| 230 | } |
| 231 | |
| 232 | /** |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 233 | * Returns true if the text contains any RTL characters, bidi format characters, or surrogate |
| 234 | * code units. |
| 235 | */ |
| 236 | private static boolean hasAnyInterestingChars(CharSequence text, int textLength) { |
| 237 | final int MAX_BUF_LEN = 500; |
| 238 | final char[] buffer = TextUtils.obtain(MAX_BUF_LEN); |
| 239 | try { |
| 240 | for (int start = 0; start < textLength; start += MAX_BUF_LEN) { |
| 241 | final int end = Math.min(start + MAX_BUF_LEN, textLength); |
| 242 | |
| 243 | // No need to worry about getting half codepoints, since we consider surrogate code |
| 244 | // units "interesting" as soon we see one. |
| 245 | TextUtils.getChars(text, start, end, buffer, 0); |
| 246 | |
| 247 | final int len = end - start; |
| 248 | for (int i = 0; i < len; i++) { |
| 249 | final char c = buffer[i]; |
Roozbeh Pournader | 8823c85 | 2016-06-09 18:36:47 -0700 | [diff] [blame] | 250 | if (c == '\n' || c == '\t' || TextUtils.couldAffectRtl(c)) { |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | return false; |
| 256 | } finally { |
| 257 | TextUtils.recycle(buffer); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 262 | * Returns null if not boring; the width, ascent, and descent in the |
| 263 | * provided Metrics object (or a new one if the provided one was null) |
| 264 | * if boring. |
| 265 | * @hide |
| 266 | */ |
| 267 | public static Metrics isBoring(CharSequence text, TextPaint paint, |
| 268 | TextDirectionHeuristic textDir, Metrics metrics) { |
Roozbeh Pournader | 3cf8208 | 2016-03-16 14:33:51 -0700 | [diff] [blame] | 269 | final int textLength = text.length(); |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 270 | if (hasAnyInterestingChars(text, textLength)) { |
| 271 | return null; // There are some interesting characters. Not boring. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 273 | if (textDir != null && textDir.isRtl(text, 0, textLength)) { |
| 274 | return null; // The heuristic considers the whole text RTL. Not boring. |
| 275 | } |
| 276 | if (text instanceof Spanned) { |
Eric Fischer | 86fcef8 | 2009-08-17 17:16:44 -0700 | [diff] [blame] | 277 | Spanned sp = (Spanned) text; |
Roozbeh Pournader | 3cf8208 | 2016-03-16 14:33:51 -0700 | [diff] [blame] | 278 | Object[] styles = sp.getSpans(0, textLength, ParagraphStyle.class); |
Eric Fischer | 86fcef8 | 2009-08-17 17:16:44 -0700 | [diff] [blame] | 279 | if (styles.length > 0) { |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 280 | return null; // There are some PargraphStyle spans. Not boring. |
Eric Fischer | 86fcef8 | 2009-08-17 17:16:44 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 284 | Metrics fm = metrics; |
| 285 | if (fm == null) { |
| 286 | fm = new Metrics(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } else { |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 288 | fm.reset(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
Roozbeh Pournader | 5536c4f | 2016-06-01 16:49:27 -0700 | [diff] [blame] | 290 | |
| 291 | TextLine line = TextLine.obtain(); |
| 292 | line.set(paint, text, 0, textLength, Layout.DIR_LEFT_TO_RIGHT, |
| 293 | Layout.DIRS_ALL_LEFT_TO_RIGHT, false, null); |
| 294 | fm.width = (int) Math.ceil(line.metrics(fm)); |
| 295 | TextLine.recycle(line); |
| 296 | |
| 297 | return fm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 300 | @Override |
| 301 | public int getHeight() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | return mBottom; |
| 303 | } |
| 304 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 305 | @Override |
| 306 | public int getLineCount() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | return 1; |
| 308 | } |
| 309 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public int getLineTop(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | if (line == 0) |
| 313 | return 0; |
| 314 | else |
| 315 | return mBottom; |
| 316 | } |
| 317 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 318 | @Override |
| 319 | public int getLineDescent(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | return mDesc; |
| 321 | } |
| 322 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 323 | @Override |
| 324 | public int getLineStart(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | if (line == 0) |
| 326 | return 0; |
| 327 | else |
| 328 | return getText().length(); |
| 329 | } |
| 330 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public int getParagraphDirection(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | return DIR_LEFT_TO_RIGHT; |
| 334 | } |
| 335 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 336 | @Override |
| 337 | public boolean getLineContainsTab(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 341 | @Override |
| 342 | public float getLineMax(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | return mMax; |
| 344 | } |
| 345 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 346 | @Override |
John Reck | 44e8d60 | 2015-10-05 15:44:04 -0700 | [diff] [blame] | 347 | public float getLineWidth(int line) { |
| 348 | return (line == 0 ? mMax : 0); |
| 349 | } |
| 350 | |
| 351 | @Override |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 352 | public final Directions getLineDirections(int line) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | return Layout.DIRS_ALL_LEFT_TO_RIGHT; |
| 354 | } |
| 355 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 356 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | public int getTopPadding() { |
| 358 | return mTopPadding; |
| 359 | } |
| 360 | |
Gilles Debunne | f483e51 | 2011-04-28 15:08:54 -0700 | [diff] [blame] | 361 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | public int getBottomPadding() { |
| 363 | return mBottomPadding; |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public int getEllipsisCount(int line) { |
| 368 | return mEllipsizedCount; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public int getEllipsisStart(int line) { |
| 373 | return mEllipsizedStart; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public int getEllipsizedWidth() { |
| 378 | return mEllipsizedWidth; |
| 379 | } |
| 380 | |
| 381 | // Override draw so it will be faster. |
| 382 | @Override |
| 383 | public void draw(Canvas c, Path highlight, Paint highlightpaint, |
| 384 | int cursorOffset) { |
| 385 | if (mDirect != null && highlight == null) { |
| 386 | c.drawText(mDirect, 0, mBottom - mDesc, mPaint); |
| 387 | } else { |
| 388 | super.draw(c, highlight, highlightpaint, cursorOffset); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Callback for the ellipsizer to report what region it ellipsized. |
| 394 | */ |
| 395 | public void ellipsized(int start, int end) { |
| 396 | mEllipsizedStart = start; |
| 397 | mEllipsizedCount = end - start; |
| 398 | } |
| 399 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | private String mDirect; |
| 401 | private Paint mPaint; |
| 402 | |
| 403 | /* package */ int mBottom, mDesc; // for Direct |
| 404 | private int mTopPadding, mBottomPadding; |
| 405 | private float mMax; |
| 406 | private int mEllipsizedWidth, mEllipsizedStart, mEllipsizedCount; |
| 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | public static class Metrics extends Paint.FontMetricsInt { |
| 409 | public int width; |
Doug Felt | e8e45f2 | 2010-03-29 14:58:40 -0700 | [diff] [blame] | 410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | @Override public String toString() { |
| 412 | return super.toString() + " width=" + width; |
| 413 | } |
Siyamed Sinir | 70f660f | 2016-03-29 11:56:53 -0700 | [diff] [blame] | 414 | |
| 415 | private void reset() { |
| 416 | top = 0; |
| 417 | bottom = 0; |
| 418 | ascent = 0; |
| 419 | descent = 0; |
| 420 | width = 0; |
| 421 | leading = 0; |
| 422 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | } |