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