Example usage for android.text Spanned nextSpanTransition

List of usage examples for android.text Spanned nextSpanTransition

Introduction

In this page you can find the example usage for android.text Spanned nextSpanTransition.

Prototype

public int nextSpanTransition(int start, int limit, Class type);

Source Link

Document

Return the first offset greater than start where a markup object of class type begins or ends, or limit if there are no starts or ends greater than start but less than limit.

Usage

From source file:org.miaowo.miaowo.util.Html.java

private static void withinDiv(StringBuilder out, Spanned text, int start, int end, int option,
        Context context) {// w  w w.j ava 2  s .c  om
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, QuoteSpan.class);
        QuoteSpan[] quotes = text.getSpans(i, next, QuoteSpan.class);

        for (QuoteSpan quote : quotes) {
            out.append("<blockquote>");
        }

        withinBlockquote(out, text, i, next, option, context);

        for (QuoteSpan quote : quotes) {
            out.append("</blockquote>\n");
        }
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option, Context context) {
    int len = text.length();

    int next;/*from   w w w  .j  a  v a2  s  .  c om*/
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align = ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option, context);

        if (needDiv) {
            out.append("</div>");
        }
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end, Context context) {
    int next;//  w  w  w .j a  v  a  2s.  com
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();

                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();

                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");

                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    sizeDip /= context.getResources().getDisplayMetrics().density;
                }

                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }

        withinStyle(out, text, i, next);

        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();

                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();

                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

private static float setPara(MeasuredText mt, TextPaint paint, CharSequence text, int start, int end,
        TextDirectionHeuristic textDir) {

    //        mt.setPara(text, start, end, textDir); //jec- ????

    float width;/*from  w ww  .  j  ava 2  s .  com*/
    Spanned sp = text instanceof Spanned ? (Spanned) text : null;
    int len = end - start;
    if (sp == null) {
        width = mt.addStyleRun(paint, len, null);
    } else {
        width = 0;
        int spanEnd;
        for (int spanStart = 0; spanStart < len; spanStart = spanEnd) {
            spanEnd = sp.nextSpanTransition(spanStart, len, MetricAffectingSpan.class);
            MetricAffectingSpan[] spans = sp.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
            spans = TextUtils.removeEmptySpans(spans, sp, MetricAffectingSpan.class);
            width += mt.addStyleRun(paint, spans, spanEnd - spanStart, null);
        }
    }

    return width;
}