Search in sources :

Example 16 with AlignmentSpan

use of android.text.style.AlignmentSpan in project android_frameworks_base by ResurrectionRemix.

the class Layout method getParagraphAlignment.

/**
     * Get the alignment of the specified paragraph, taking into account
     * markup attached to it.
     */
public final Alignment getParagraphAlignment(int line) {
    Alignment align = mAlignment;
    if (mSpannedText) {
        Spanned sp = (Spanned) mText;
        AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line), getLineEnd(line), AlignmentSpan.class);
        int spanLength = spans.length;
        if (spanLength > 0) {
            align = spans[spanLength - 1].getAlignment();
        }
    }
    return align;
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) Paint(android.graphics.Paint)

Example 17 with AlignmentSpan

use of android.text.style.AlignmentSpan in project android_frameworks_base by DirtyUnicorns.

the class HtmlToSpannedConverter method getTextStyles.

private static String getTextStyles(Spanned text, int start, int end, boolean forceNoVerticalMargin, boolean includeTextAlign) {
    String margin = null;
    String textAlign = null;
    if (forceNoVerticalMargin) {
        margin = "margin-top:0; margin-bottom:0;";
    }
    if (includeTextAlign) {
        final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
        // Only use the last AlignmentSpan with flag SPAN_PARAGRAPH
        for (int i = alignmentSpans.length - 1; i >= 0; i--) {
            AlignmentSpan s = alignmentSpans[i];
            if ((text.getSpanFlags(s) & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH) {
                final Layout.Alignment alignment = s.getAlignment();
                if (alignment == Layout.Alignment.ALIGN_NORMAL) {
                    textAlign = "text-align:start;";
                } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
                    textAlign = "text-align:center;";
                } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
                    textAlign = "text-align:end;";
                }
                break;
            }
        }
    }
    if (margin == null && textAlign == null) {
        return "";
    }
    final StringBuilder style = new StringBuilder(" style=\"");
    if (margin != null && textAlign != null) {
        style.append(margin).append(" ").append(textAlign);
    } else if (margin != null) {
        style.append(margin);
    } else if (textAlign != null) {
        style.append(textAlign);
    }
    return style.append("\"").toString();
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan)

Example 18 with AlignmentSpan

use of android.text.style.AlignmentSpan in project android_frameworks_base by DirtyUnicorns.

the class Layout method getParagraphAlignment.

/**
     * Get the alignment of the specified paragraph, taking into account
     * markup attached to it.
     */
public final Alignment getParagraphAlignment(int line) {
    Alignment align = mAlignment;
    if (mSpannedText) {
        Spanned sp = (Spanned) mText;
        AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line), getLineEnd(line), AlignmentSpan.class);
        int spanLength = spans.length;
        if (spanLength > 0) {
            align = spans[spanLength - 1].getAlignment();
        }
    }
    return align;
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) Paint(android.graphics.Paint)

Example 19 with AlignmentSpan

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

the class HtmlToSpannedConverter method getTextStyles.

private static String getTextStyles(Spanned text, int start, int end, boolean forceNoVerticalMargin, boolean includeTextAlign) {
    String margin = null;
    String textAlign = null;
    if (forceNoVerticalMargin) {
        margin = "margin-top:0; margin-bottom:0;";
    }
    if (includeTextAlign) {
        final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
        // Only use the last AlignmentSpan with flag SPAN_PARAGRAPH
        for (int i = alignmentSpans.length - 1; i >= 0; i--) {
            AlignmentSpan s = alignmentSpans[i];
            if ((text.getSpanFlags(s) & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH) {
                final Layout.Alignment alignment = s.getAlignment();
                if (alignment == Layout.Alignment.ALIGN_NORMAL) {
                    textAlign = "text-align:start;";
                } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
                    textAlign = "text-align:center;";
                } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
                    textAlign = "text-align:end;";
                }
                break;
            }
        }
    }
    if (margin == null && textAlign == null) {
        return "";
    }
    final StringBuilder style = new StringBuilder(" style=\"");
    if (margin != null && textAlign != null) {
        style.append(margin).append(" ").append(textAlign);
    } else if (margin != null) {
        style.append(margin);
    } else if (textAlign != null) {
        style.append(textAlign);
    }
    return style.append("\"").toString();
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan)

Example 20 with AlignmentSpan

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

the class Layout method drawText.

/**
     * @hide
     */
public void drawText(Canvas canvas, int firstLine, int lastLine) {
    int previousLineBottom = getLineTop(firstLine);
    int previousLineEnd = getLineStart(firstLine);
    ParagraphStyle[] spans = NO_PARA_SPANS;
    int spanEnd = 0;
    TextPaint paint = mPaint;
    CharSequence buf = mText;
    Alignment paraAlign = mAlignment;
    TabStops tabStops = null;
    boolean tabStopsIsInitialized = false;
    TextLine tl = TextLine.obtain();
    // The baseline is the top of the following line minus the current line's descent.
    for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
        int start = previousLineEnd;
        previousLineEnd = getLineStart(lineNum + 1);
        int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
        int ltop = previousLineBottom;
        int lbottom = getLineTop(lineNum + 1);
        previousLineBottom = lbottom;
        int lbaseline = lbottom - getLineDescent(lineNum);
        int dir = getParagraphDirection(lineNum);
        int left = 0;
        int right = mWidth;
        if (mSpannedText) {
            Spanned sp = (Spanned) buf;
            int textLength = buf.length();
            boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
            // our problem.
            if (start >= spanEnd && (lineNum == firstLine || 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;
            boolean useFirstLineMargin = isFirstParaLine;
            for (int n = 0; n < length; n++) {
                if (spans[n] instanceof LeadingMarginSpan2) {
                    int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
                    int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
                    // the count that is greatest
                    if (lineNum < startLine + count) {
                        useFirstLineMargin = true;
                        break;
                    }
                }
            }
            for (int n = 0; n < length; n++) {
                if (spans[n] instanceof LeadingMarginSpan) {
                    LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
                    if (dir == DIR_RIGHT_TO_LEFT) {
                        margin.drawLeadingMargin(canvas, paint, right, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
                        right -= margin.getLeadingMargin(useFirstLineMargin);
                    } else {
                        margin.drawLeadingMargin(canvas, paint, left, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
                        left += margin.getLeadingMargin(useFirstLineMargin);
                    }
                }
            }
        }
        boolean hasTab = getLineContainsTab(lineNum);
        // Can't tell if we have tabs for sure, currently
        if (hasTab && !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 + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
            } else {
                x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
            }
        } else {
            int max = (int) getLineExtent(lineNum, tabStops, false);
            if (align == Alignment.ALIGN_OPPOSITE) {
                if (dir == DIR_LEFT_TO_RIGHT) {
                    x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
                } else {
                    x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
                }
            } else {
                // Alignment.ALIGN_CENTER
                max = max & ~1;
                x = ((right + left - max) >> 1) + getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
            }
        }
        paint.setHyphenEdit(getHyphen(lineNum));
        Directions directions = getLineDirections(lineNum);
        if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
            // XXX: assumes there's nothing additional to be done
            canvas.drawText(buf, start, end, x, lbaseline, paint);
        } else {
            tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
            tl.draw(canvas, x, ltop, lbaseline, lbottom);
        }
        paint.setHyphenEdit(0);
    }
    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)

Aggregations

AlignmentSpan (android.text.style.AlignmentSpan)20 Paint (android.graphics.Paint)14 LeadingMarginSpan (android.text.style.LeadingMarginSpan)7 LeadingMarginSpan2 (android.text.style.LeadingMarginSpan.LeadingMarginSpan2)7 ParagraphStyle (android.text.style.ParagraphStyle)7 Context (android.content.Context)1 Drawable (android.graphics.drawable.Drawable)1 ImageSpan (android.text.style.ImageSpan)1 LineBackgroundSpan (android.text.style.LineBackgroundSpan)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 WPImageGetter (org.wordpress.android.util.helpers.WPImageGetter)1