FileDocCategorySizeDatePackage
BoringLayout.javaAPI DocAndroid 5.1 API14149Thu Mar 12 22:22:10 GMT 2015android.text

BoringLayout

public class BoringLayout extends Layout implements TextUtils.EllipsizeCallback
A BoringLayout is a very simple Layout implementation for text that fits on a single line and is all left-to-right characters. You will probably never want to make one of these yourself; if you do, be sure to call {@link #isBoring} first to make sure the text meets the criteria.

This class is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, in which case you are encouraged to use a Layout instead of calling {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, float, float, android.graphics.Paint) Canvas.drawText()} directly.

Fields Summary
private static final char
FIRST_RIGHT_TO_LEFT
private String
mDirect
private android.graphics.Paint
mPaint
int
mBottom
int
mDesc
private int
mTopPadding
private int
mBottomPadding
private float
mMax
private int
mEllipsizedWidth
private int
mEllipsizedStart
private int
mEllipsizedCount
private static final TextPaint
sTemp
Constructors Summary
public BoringLayout(CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, Metrics metrics, boolean includepad)

        super(source, paint, outerwidth, align, spacingmult, spacingadd);

        mEllipsizedWidth = outerwidth;
        mEllipsizedStart = 0;
        mEllipsizedCount = 0;

        init(source, paint, outerwidth, align, spacingmult, spacingadd,
             metrics, includepad, true);
    
public BoringLayout(CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, Metrics metrics, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth)

        /*
         * It is silly to have to call super() and then replaceWith(),
         * but we can't use "this" for the callback until the call to
         * super() finishes.
         */
        super(source, paint, outerwidth, align, spacingmult, spacingadd);

        boolean trust;

        if (ellipsize == null || ellipsize == TextUtils.TruncateAt.MARQUEE) {
            mEllipsizedWidth = outerwidth;
            mEllipsizedStart = 0;
            mEllipsizedCount = 0;
            trust = true;
        } else {
            replaceWith(TextUtils.ellipsize(source, paint, ellipsizedWidth,
                                           ellipsize, true, this),
                        paint, outerwidth, align, spacingmult,
                        spacingadd);


            mEllipsizedWidth = ellipsizedWidth;
            trust = false;
        }

        init(getText(), paint, outerwidth, align, spacingmult, spacingadd,
             metrics, includepad, trust);
    
Methods Summary
public voiddraw(android.graphics.Canvas c, android.graphics.Path highlight, android.graphics.Paint highlightpaint, int cursorOffset)

        if (mDirect != null && highlight == null) {
            c.drawText(mDirect, 0, mBottom - mDesc, mPaint);
        } else {
            super.draw(c, highlight, highlightpaint, cursorOffset);
        }
    
public voidellipsized(int start, int end)
Callback for the ellipsizer to report what region it ellipsized.

        mEllipsizedStart = start;
        mEllipsizedCount = end - start;
    
public intgetBottomPadding()

        return mBottomPadding;
    
public intgetEllipsisCount(int line)

        return mEllipsizedCount;
    
public intgetEllipsisStart(int line)

        return mEllipsizedStart;
    
public intgetEllipsizedWidth()

        return mEllipsizedWidth;
    
public intgetHeight()

        return mBottom;
    
public booleangetLineContainsTab(int line)

        return false;
    
public intgetLineCount()

        return 1;
    
public intgetLineDescent(int line)

        return mDesc;
    
public final DirectionsgetLineDirections(int line)

        return Layout.DIRS_ALL_LEFT_TO_RIGHT;
    
public floatgetLineMax(int line)

        return mMax;
    
public intgetLineStart(int line)

        if (line == 0)
            return 0;
        else
            return getText().length();
    
public intgetLineTop(int line)

        if (line == 0)
            return 0;
        else
            return mBottom;
    
public intgetParagraphDirection(int line)

        return DIR_LEFT_TO_RIGHT;
    
public intgetTopPadding()

        return mTopPadding;
    
voidinit(java.lang.CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, android.text.BoringLayout$Metrics metrics, boolean includepad, boolean trustWidth)

        int spacing;

        if (source instanceof String && align == Layout.Alignment.ALIGN_NORMAL) {
            mDirect = source.toString();
        } else {
            mDirect = null;
        }

        mPaint = paint;

        if (includepad) {
            spacing = metrics.bottom - metrics.top;
        } else {
            spacing = metrics.descent - metrics.ascent;
        }

        mBottom = spacing;

        if (includepad) {
            mDesc = spacing + metrics.top;
        } else {
            mDesc = spacing + metrics.ascent;
        }

        if (trustWidth) {
            mMax = metrics.width;
        } else {
            /*
             * If we have ellipsized, we have to actually calculate the
             * width because the width that was passed in was for the
             * full text, not the ellipsized form.
             */
            TextLine line = TextLine.obtain();
            line.set(paint, source, 0, source.length(), Layout.DIR_LEFT_TO_RIGHT,
                    Layout.DIRS_ALL_LEFT_TO_RIGHT, false, null);
            mMax = (int) FloatMath.ceil(line.metrics(null));
            TextLine.recycle(line);
        }

        if (includepad) {
            mTopPadding = metrics.top - metrics.ascent;
            mBottomPadding = metrics.bottom - metrics.descent;
        }
    
public static android.text.BoringLayout$MetricsisBoring(java.lang.CharSequence text, TextPaint paint, android.text.BoringLayout$Metrics metrics)
Returns null if not boring; the width, ascent, and descent in the provided Metrics object (or a new one if the provided one was null) if boring.

        return isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, metrics);
    
public static android.text.BoringLayout$MetricsisBoring(java.lang.CharSequence text, TextPaint paint, TextDirectionHeuristic textDir, android.text.BoringLayout$Metrics metrics)
Returns null if not boring; the width, ascent, and descent in the provided Metrics object (or a new one if the provided one was null) if boring.

hide

        char[] temp = TextUtils.obtain(500);
        int length = text.length();
        boolean boring = true;

        outer:
        for (int i = 0; i < length; i += 500) {
            int j = i + 500;

            if (j > length)
                j = length;

            TextUtils.getChars(text, i, j, temp, 0);

            int n = j - i;

            for (int a = 0; a < n; a++) {
                char c = temp[a];

                if (c == '\n" || c == '\t" || c >= FIRST_RIGHT_TO_LEFT) {
                    boring = false;
                    break outer;
                }
            }

            if (textDir != null && textDir.isRtl(temp, 0, n)) {
               boring = false;
               break outer;
            }
        }

        TextUtils.recycle(temp);

        if (boring && text instanceof Spanned) {
            Spanned sp = (Spanned) text;
            Object[] styles = sp.getSpans(0, length, ParagraphStyle.class);
            if (styles.length > 0) {
                boring = false;
            }
        }

        if (boring) {
            Metrics fm = metrics;
            if (fm == null) {
                fm = new Metrics();
            }

            TextLine line = TextLine.obtain();
            line.set(paint, text, 0, length, Layout.DIR_LEFT_TO_RIGHT,
                    Layout.DIRS_ALL_LEFT_TO_RIGHT, false, null);
            fm.width = (int) FloatMath.ceil(line.metrics(fm));
            TextLine.recycle(line);

            return fm;
        } else {
            return null;
        }
    
public static android.text.BoringLayout$MetricsisBoring(java.lang.CharSequence text, TextPaint paint)
Returns null if not boring; the width, ascent, and descent if boring.

        return isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, null);
    
public static android.text.BoringLayout$MetricsisBoring(java.lang.CharSequence text, TextPaint paint, TextDirectionHeuristic textDir)
Returns null if not boring; the width, ascent, and descent if boring.

hide

        return isBoring(text, paint, textDir, null);
    
public static android.text.BoringLayoutmake(java.lang.CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, android.text.BoringLayout$Metrics metrics, boolean includepad)

        return new BoringLayout(source, paint, outerwidth, align,
                                spacingmult, spacingadd, metrics,
                                includepad);
    
public static android.text.BoringLayoutmake(java.lang.CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, android.text.BoringLayout$Metrics metrics, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth)

        return new BoringLayout(source, paint, outerwidth, align,
                                spacingmult, spacingadd, metrics,
                                includepad, ellipsize, ellipsizedWidth);
    
public android.text.BoringLayoutreplaceOrMake(java.lang.CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, android.text.BoringLayout$Metrics metrics, boolean includepad)
Returns a BoringLayout for the specified text, potentially reusing this one if it is already suitable. The caller must make sure that no one is still using this Layout.

        replaceWith(source, paint, outerwidth, align, spacingmult,
                    spacingadd);

        mEllipsizedWidth = outerwidth;
        mEllipsizedStart = 0;
        mEllipsizedCount = 0;

        init(source, paint, outerwidth, align, spacingmult, spacingadd,
             metrics, includepad, true);
        return this;
    
public android.text.BoringLayoutreplaceOrMake(java.lang.CharSequence source, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, android.text.BoringLayout$Metrics metrics, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth)
Returns a BoringLayout for the specified text, potentially reusing this one if it is already suitable. The caller must make sure that no one is still using this Layout.

        boolean trust;

        if (ellipsize == null || ellipsize == TextUtils.TruncateAt.MARQUEE) {
            replaceWith(source, paint, outerwidth, align, spacingmult,
                        spacingadd);

            mEllipsizedWidth = outerwidth;
            mEllipsizedStart = 0;
            mEllipsizedCount = 0;
            trust = true;
        } else {
            replaceWith(TextUtils.ellipsize(source, paint, ellipsizedWidth,
                                           ellipsize, true, this),
                        paint, outerwidth, align, spacingmult,
                        spacingadd);

            mEllipsizedWidth = ellipsizedWidth;
            trust = false;
        }

        init(getText(), paint, outerwidth, align, spacingmult, spacingadd,
             metrics, includepad, trust);
        return this;