Search in sources :

Example 6 with LineBackgroundSpan

use of android.text.style.LineBackgroundSpan in project android_frameworks_base by crdroidandroid.

the class Layout method drawBackground.

/**
     * @hide
     */
public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint, int cursorOffsetVertical, int firstLine, int lastLine) {
    // They are evaluated at each line.
    if (mSpannedText) {
        if (mLineBackgroundSpans == null) {
            mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
        }
        Spanned buffer = (Spanned) mText;
        int textLength = buffer.length();
        mLineBackgroundSpans.init(buffer, 0, textLength);
        if (mLineBackgroundSpans.numberOfSpans > 0) {
            int previousLineBottom = getLineTop(firstLine);
            int previousLineEnd = getLineStart(firstLine);
            ParagraphStyle[] spans = NO_PARA_SPANS;
            int spansLength = 0;
            TextPaint paint = mPaint;
            int spanEnd = 0;
            final int width = mWidth;
            for (int i = firstLine; i <= lastLine; i++) {
                int start = previousLineEnd;
                int end = getLineStart(i + 1);
                previousLineEnd = end;
                int ltop = previousLineBottom;
                int lbottom = getLineTop(i + 1);
                previousLineBottom = lbottom;
                int lbaseline = lbottom - getLineDescent(i);
                if (start >= spanEnd) {
                    // These should be infrequent, so we'll use this so that
                    // we don't have to check as often.
                    spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
                    // All LineBackgroundSpans on a line contribute to its background.
                    spansLength = 0;
                    // Duplication of the logic of getParagraphSpans
                    if (start != end || start == 0) {
                        // array instead to reduce memory allocation
                        for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
                            // construction
                            if (mLineBackgroundSpans.spanStarts[j] >= end || mLineBackgroundSpans.spanEnds[j] <= start)
                                continue;
                            spans = GrowingArrayUtils.append(spans, spansLength, mLineBackgroundSpans.spans[j]);
                            spansLength++;
                        }
                    }
                }
                for (int n = 0; n < spansLength; n++) {
                    LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
                    lineBackgroundSpan.drawBackground(canvas, paint, 0, width, ltop, lbaseline, lbottom, buffer, start, end, i);
                }
            }
        }
        mLineBackgroundSpans.recycle();
    }
    // a non-spanned transformation of a spanned editing buffer.
    if (highlight != null) {
        if (cursorOffsetVertical != 0)
            canvas.translate(0, cursorOffsetVertical);
        canvas.drawPath(highlight, highlightPaint);
        if (cursorOffsetVertical != 0)
            canvas.translate(0, -cursorOffsetVertical);
    }
}
Also used : LineBackgroundSpan(android.text.style.LineBackgroundSpan) ParagraphStyle(android.text.style.ParagraphStyle) Paint(android.graphics.Paint)

Example 7 with LineBackgroundSpan

use of android.text.style.LineBackgroundSpan in project XobotOS by xamarin.

the class Layout method draw.

/**
     * Draw this Layout on the specified canvas, with the highlight path drawn
     * between the background and the text.
     *
     * @param c the canvas
     * @param highlight the path of the highlight or cursor; can be null
     * @param highlightPaint the paint for the highlight
     * @param cursorOffsetVertical the amount to temporarily translate the
     *        canvas while rendering the highlight
     */
public void draw(Canvas c, Path highlight, Paint highlightPaint, int cursorOffsetVertical) {
    int dtop, dbottom;
    synchronized (sTempRect) {
        if (!c.getClipBounds(sTempRect)) {
            return;
        }
        dtop = sTempRect.top;
        dbottom = sTempRect.bottom;
    }
    int top = 0;
    int bottom = getLineTop(getLineCount());
    if (dtop > top) {
        top = dtop;
    }
    if (dbottom < bottom) {
        bottom = dbottom;
    }
    int first = getLineForVertical(top);
    int last = getLineForVertical(bottom);
    int previousLineBottom = getLineTop(first);
    int previousLineEnd = getLineStart(first);
    TextPaint paint = mPaint;
    CharSequence buf = mText;
    int width = mWidth;
    boolean spannedText = mSpannedText;
    ParagraphStyle[] spans = NO_PARA_SPANS;
    int spanEnd = 0;
    int textLength = 0;
    // They are evaluated at each line.
    if (spannedText) {
        Spanned sp = (Spanned) buf;
        textLength = buf.length();
        for (int i = first; i <= last; i++) {
            int start = previousLineEnd;
            int end = getLineStart(i + 1);
            previousLineEnd = end;
            int ltop = previousLineBottom;
            int lbottom = getLineTop(i + 1);
            previousLineBottom = lbottom;
            int lbaseline = lbottom - getLineDescent(i);
            if (start >= spanEnd) {
                // These should be infrequent, so we'll use this so that
                // we don't have to check as often.
                spanEnd = sp.nextSpanTransition(start, textLength, LineBackgroundSpan.class);
                // All LineBackgroundSpans on a line contribute to its
                // background.
                spans = getParagraphSpans(sp, start, end, LineBackgroundSpan.class);
            }
            for (int n = 0; n < spans.length; n++) {
                LineBackgroundSpan back = (LineBackgroundSpan) spans[n];
                back.drawBackground(c, paint, 0, width, ltop, lbaseline, lbottom, buf, start, end, i);
            }
        }
        // reset to their original values
        spanEnd = 0;
        previousLineBottom = getLineTop(first);
        previousLineEnd = getLineStart(first);
        spans = NO_PARA_SPANS;
    }
    // a non-spanned transformation of a spanned editing buffer.
    if (highlight != null) {
        if (cursorOffsetVertical != 0) {
            c.translate(0, cursorOffsetVertical);
        }
        c.drawPath(highlight, highlightPaint);
        if (cursorOffsetVertical != 0) {
            c.translate(0, -cursorOffsetVertical);
        }
    }
    Alignment paraAlign = mAlignment;
    TabStops tabStops = null;
    boolean tabStopsIsInitialized = false;
    TextLine tl = TextLine.obtain();
    // line's descent.
    for (int i = first; i <= last; i++) {
        int start = previousLineEnd;
        previousLineEnd = getLineStart(i + 1);
        int end = getLineVisibleEnd(i, start, previousLineEnd);
        int ltop = previousLineBottom;
        int lbottom = getLineTop(i + 1);
        previousLineBottom = lbottom;
        int lbaseline = lbottom - getLineDescent(i);
        int dir = getParagraphDirection(i);
        int left = 0;
        int right = mWidth;
        if (spannedText) {
            Spanned sp = (Spanned) buf;
            boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
            // our problem.
            if (start >= spanEnd && (i == first || isFirstParaLine)) {
                spanEnd = sp.nextSpanTransition(start, textLength, ParagraphStyle.class);
                spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
                paraAlign = mAlignment;
                for (int n = spans.length - 1; n >= 0; n--) {
                    if (spans[n] instanceof AlignmentSpan) {
                        paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
                        break;
                    }
                }
                tabStopsIsInitialized = false;
            }
            // Draw all leading margin spans.  Adjust left or right according
            // to the paragraph direction of the line.
            final int length = spans.length;
            for (int n = 0; n < length; n++) {
                if (spans[n] instanceof LeadingMarginSpan) {
                    LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
                    boolean useFirstLineMargin = isFirstParaLine;
                    if (margin instanceof LeadingMarginSpan2) {
                        int count = ((LeadingMarginSpan2) margin).getLeadingMarginLineCount();
                        int startLine = getLineForOffset(sp.getSpanStart(margin));
                        useFirstLineMargin = i < startLine + count;
                    }
                    if (dir == DIR_RIGHT_TO_LEFT) {
                        margin.drawLeadingMargin(c, paint, right, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
                        right -= margin.getLeadingMargin(useFirstLineMargin);
                    } else {
                        margin.drawLeadingMargin(c, paint, left, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
                        left += margin.getLeadingMargin(useFirstLineMargin);
                    }
                }
            }
        }
        boolean hasTabOrEmoji = getLineContainsTab(i);
        // Can't tell if we have tabs for sure, currently
        if (hasTabOrEmoji && !tabStopsIsInitialized) {
            if (tabStops == null) {
                tabStops = new TabStops(TAB_INCREMENT, spans);
            } else {
                tabStops.reset(TAB_INCREMENT, spans);
            }
            tabStopsIsInitialized = true;
        }
        // Determine whether the line aligns to normal, opposite, or center.
        Alignment align = paraAlign;
        if (align == Alignment.ALIGN_LEFT) {
            align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
        } else if (align == Alignment.ALIGN_RIGHT) {
            align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
        }
        int x;
        if (align == Alignment.ALIGN_NORMAL) {
            if (dir == DIR_LEFT_TO_RIGHT) {
                x = left;
            } else {
                x = right;
            }
        } else {
            int max = (int) getLineExtent(i, tabStops, false);
            if (align == Alignment.ALIGN_OPPOSITE) {
                if (dir == DIR_LEFT_TO_RIGHT) {
                    x = right - max;
                } else {
                    x = left - max;
                }
            } else {
                // Alignment.ALIGN_CENTER
                max = max & ~1;
                x = (right + left - max) >> 1;
            }
        }
        Directions directions = getLineDirections(i);
        if (directions == DIRS_ALL_LEFT_TO_RIGHT && !spannedText && !hasTabOrEmoji) {
            // XXX: assumes there's nothing additional to be done
            c.drawText(buf, start, end, x, lbaseline, paint);
        } else {
            tl.set(paint, buf, start, end, dir, directions, hasTabOrEmoji, tabStops);
            tl.draw(c, x, ltop, lbaseline, lbottom);
        }
    }
    TextLine.recycle(tl);
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) ParagraphStyle(android.text.style.ParagraphStyle) Paint(android.graphics.Paint) LeadingMarginSpan2(android.text.style.LeadingMarginSpan.LeadingMarginSpan2) LeadingMarginSpan(android.text.style.LeadingMarginSpan) LineBackgroundSpan(android.text.style.LineBackgroundSpan)

Aggregations

Paint (android.graphics.Paint)7 LineBackgroundSpan (android.text.style.LineBackgroundSpan)7 ParagraphStyle (android.text.style.ParagraphStyle)7 AlignmentSpan (android.text.style.AlignmentSpan)1 LeadingMarginSpan (android.text.style.LeadingMarginSpan)1 LeadingMarginSpan2 (android.text.style.LeadingMarginSpan.LeadingMarginSpan2)1