Java Code Examples for android.text.Spanned#SPAN_PARAGRAPH

The following examples show how to use android.text.Spanned#SPAN_PARAGRAPH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpannableStringUtils.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example 2
Source File: SpannableStringUtils.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example 3
Source File: SpannableStringUtils.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
 *
 * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
 * kind of span that is copied.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
        Spannable dest, int destoff) {
    Object[] spans = source.getSpans(start, end, SuggestionSpan.class);

    for (int i = 0; i < spans.length; i++) {
        int fl = source.getSpanFlags(spans[i]);
        // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
        // is set, Spannable#setSpan will throw an exception unless the span is on the edge
        // of a word. But the spans have been split into two by the getText{Before,After}Cursor
        // methods, so after concatenation they may end in the middle of a word.
        // Since we don't use them, we can just remove them and avoid crashing.
        fl &= ~Spanned.SPAN_PARAGRAPH;

        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example 4
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 5
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: Html.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
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();
}
 
Example 7
Source File: Html.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
private static void withinBlockquoteIndividual(StringBuilder out,
                                                   Spanned text,
                                                   int start,
                                                   int end) {
        boolean isInList = false;
        int next;
        for (int i = start; i <= end; i = next) {
            next = TextUtils.indexOf(text, '\n', i, end);
            if (next < 0) {
                next = end;
            }

            if (next == i) {
                if (isInList) {
                    // Current paragraph is no longer a list item; close the previously opened list
                    isInList = false;
                    // xxx
//                    out.append("</ul>\n");
                    out.append("</ul>");
                }
                // xxx
//                out.append("<br>\n");
                out.append("<br>");
            } else {
                boolean isListItem = false;
                IBlockStyle[] blockStyles = text.getSpans(i, next, IBlockStyle.class);
                for (IBlockStyle blockStyle : blockStyles) {
                    final int spanFlags = text.getSpanFlags(blockStyle);
                    if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                            && blockStyle instanceof BulletSpan) {
                        isListItem = true;
                        break;
                    }
                }

                if (isListItem && !isInList) {
                    // Current paragraph is the first item in a list
                    isInList = true;
                    // xxx
//                    out.append("<ul")
//                            .append(getTextStyles(text, i, next, true, false))
//                            .append(">\n");
                    out.append("<ul")
                            .append(getTextStyles(text, i, next, true, false))
                            .append(">");
                }

                if (isInList && !isListItem) {
                    // Current paragraph is no longer a list item; close the previously opened list
                    isInList = false;
                    // xxx
//                    out.append("</ul>\n");
                    out.append("</ul>");

                }

                String tagType = isListItem ? "li" : "p";
                out.append("<").append(tagType)
                        .append(getTextDirection(text, i, next))
                        .append(getTextStyles(text, i, next, !isListItem, true))
                        .append(">");

                withinParagraph(out, text, i, next);

                out.append("</");
                out.append(tagType);
                // xxx
//                out.append(">\n");
                out.append(">");

                if (next == end && isInList) {
                    isInList = false;
                    // xxx
//                    out.append("</ul>\n");
                    out.append("</ul>");
                }
            }

            next++;
        }
    }
 
Example 8
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
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();
}
 
Example 9
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private /* static */ void withinBlockquoteIndividual(StringBuilder out, Spanned text, int start,
                                               int end) {
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        if (next == i) {
            if (isInList) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            out.append("<br>\n");
        } else {
            boolean isListItem = false;
            ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
            for (ParagraphStyle paragraphStyle : paragraphStyles) {
                final int spanFlags = text.getSpanFlags(paragraphStyle);
                if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                        && paragraphStyle instanceof BulletSpan) {
                    isListItem = true;
                    break;
                }
            }

            if (isListItem && !isInList) {
                // Current paragraph is the first item in a list
                isInList = true;
                out.append("<ul")
                        .append(getTextStyles(text, i, next, true, false))
                        .append(">\n");
            }

            if (isInList && !isListItem) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }

            String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType)
                    .append(getTextDirection(text, i, next))
                    .append(getTextStyles(text, i, next, !isListItem, true))
                    .append(">");

            withinParagraph(out, text, i, next);

            out.append("</");
            out.append(tagType);
            out.append(">\n");

            if (next == end && isInList) {
                isInList = false;
                out.append("</ul>\n");
            }
        }

        next++;
    }
}
 
Example 10
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 4 votes vote down vote up
private static void withinBlockquoteIndividual(Context context, StringBuilder out, Spanned text,
                                               int start, int end) {
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }
        if (next == i) {
            if (isInList) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            out.append("<br>\n");
        } else {
            boolean isListItem = false;
            ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
            for (ParagraphStyle paragraphStyle : paragraphStyles) {
                final int spanFlags = text.getSpanFlags(paragraphStyle);
                if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                        && paragraphStyle instanceof BulletSpan) {
                    isListItem = true;
                    break;
                }
            }
            if (isListItem && !isInList) {
                // Current paragraph is the first item in a list
                isInList = true;
                out.append("<ul")
                   .append(getTextStyles(text, i, next, true, false))
                   .append(">\n");
            }
            if (isInList && !isListItem) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType)
               .append(getTextDirection(text, i, next))
               .append(getTextStyles(text, i, next, !isListItem, true))
               .append(">");
            withinParagraph(context, out, text, i, next);
            out.append("</");
            out.append(tagType);
            out.append(">\n");
            if (next == end && isInList) {
                isInList = false;
                out.append("</ul>\n");
            }
        }
        next++;
    }
}
 
Example 11
Source File: Html.java    From ForPDA with GNU General Public License v3.0 4 votes vote down vote up
private static void withinBlockquoteIndividual(StringBuilder out, Spanned text, int start,
                                               int end) {
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }
        if (next == i) {
            if (isInList) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            out.append("<br>\n");
        } else {
            boolean isListItem = false;
            ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
            for (ParagraphStyle paragraphStyle : paragraphStyles) {
                final int spanFlags = text.getSpanFlags(paragraphStyle);
                if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                        && paragraphStyle instanceof BulletSpan) {
                    isListItem = true;
                    break;
                }
            }
            if (isListItem && !isInList) {
                // Current paragraph is the first item in a list
                isInList = true;
                out.append("<ul")
                        .append(getTextStyles(text, i, next, true, false))
                        .append(">\n");
            }
            if (isInList && !isListItem) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType)
                    .append(getTextDirection(text, i, next))
                    .append(getTextStyles(text, i, next, !isListItem, true))
                    .append(">");
            withinParagraph(out, text, i, next);
            out.append("</");
            out.append(tagType);
            out.append(">\n");
            if (next == end && isInList) {
                isInList = false;
                out.append("</ul>\n");
            }
        }
        next++;
    }
}
 
Example 12
Source File: AKHtml.java    From Mupdf with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example 13
Source File: AKHtml.java    From Mupdf with Apache License 2.0 4 votes vote down vote up
private static void withinBlockquoteIndividual(StringBuilder out, Spanned text, int start,
                                               int end) {
    boolean isInList = false;
    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(text, '\n', i, end);
        if (next < 0) {
            next = end;
        }

        if (next == i) {
            if (isInList) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }
            out.append("<br>\n");
        } else {
            boolean isListItem = false;
            ParagraphStyle[] paragraphStyles = text.getSpans(i, next, ParagraphStyle.class);
            for (ParagraphStyle paragraphStyle : paragraphStyles) {
                final int spanFlags = text.getSpanFlags(paragraphStyle);
                if ((spanFlags & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH
                        && paragraphStyle instanceof BulletSpan) {
                    isListItem = true;
                    break;
                }
            }

            if (isListItem && !isInList) {
                // Current paragraph is the first item in a list
                isInList = true;
                out.append("<ul")
                        .append(getTextStyles(text, i, next, true, false))
                        .append(">\n");
            }

            if (isInList && !isListItem) {
                // Current paragraph is no longer a list item; close the previously opened list
                isInList = false;
                out.append("</ul>\n");
            }

            String tagType = isListItem ? "li" : "p";
            out.append("<").append(tagType)
                    .append(getTextDirection(text, i, next))
                    .append(getTextStyles(text, i, next, !isListItem, true))
                    .append(">");

            withinParagraph(out, text, i, next);

            out.append("</");
            out.append(tagType);
            out.append(">\n");

            if (next == end && isInList) {
                isInList = false;
                out.append("</ul>\n");
            }
        }

        next++;
    }
}