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