Java Code Examples for android.text.Spanned#getSpanFlags()

The following examples show how to use android.text.Spanned#getSpanFlags() . 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 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 3
Source File: ClonedSpannableString.java    From Android-RTEditor with Apache License 2.0 6 votes vote down vote up
private void init(CharSequence source, int start, int end) {
    int initial = 20;
    mSpans = new Object[initial];
    mSpanData = new int[initial * 3];

    if (source instanceof Spanned) {
        Spanned sp = (Spanned) source;
        for (Object span : sp.getSpans(start, end, Object.class)) {
            if (span instanceof CharacterStyle || span instanceof ParagraphStyle) {
                int st = sp.getSpanStart(span);
                int en = sp.getSpanEnd(span);
                int fl = sp.getSpanFlags(span);

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

                setSpan(span, st - start, en - start, fl);
            }
        }
    }
}
 
Example 4
Source File: ClonedSpannableString.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void init(CharSequence source, int start, int end) {
    int initial = 20;
    mSpans = new Object[initial];
    mSpanData = new int[initial * 3];

    if (source instanceof Spanned) {
        Spanned sp = (Spanned) source;
        for (Object span : sp.getSpans(start, end, Object.class)) {
            if (span instanceof CharacterStyle || span instanceof ParagraphStyle) {
                int st = sp.getSpanStart(span);
                int en = sp.getSpanEnd(span);
                int fl = sp.getSpanFlags(span);

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

                setSpan(span, st - start, en - start, fl);
            }
        }
    }
}
 
Example 5
Source File: ClonedSpannableString.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void init(CharSequence source, int start, int end) {
    int initial = 20;
    mSpans = new Object[initial];
    mSpanData = new int[initial * 3];

    if (source instanceof Spanned) {
        Spanned sp = (Spanned) source;
        for (Object span : sp.getSpans(start, end, Object.class)) {
            if (span instanceof CharacterStyle || span instanceof ParagraphStyle) {
                int st = sp.getSpanStart(span);
                int en = sp.getSpanEnd(span);
                int fl = sp.getSpanFlags(span);

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

                setSpan(span, st - start, en - start, fl);
            }
        }
    }
}
 
Example 6
Source File: SpannableStringUtilsTests.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
private static void assertSpan(final CharSequence cs, final Object expectedSpan,
        final int expectedStart, final int expectedEnd, final int expectedFlags) {
    assertTrue(cs instanceof Spanned);
    final Spanned spanned = (Spanned) cs;
    final Object[] actualSpans = spanned.getSpans(0, spanned.length(), Object.class);
    for (Object actualSpan : actualSpans) {
        if (actualSpan == expectedSpan) {
            final int actualStart = spanned.getSpanStart(actualSpan);
            final int actualEnd = spanned.getSpanEnd(actualSpan);
            final int actualFlags = spanned.getSpanFlags(actualSpan);
            assertEquals(expectedStart, actualStart);
            assertEquals(expectedEnd, actualEnd);
            assertEquals(expectedFlags, actualFlags);
            return;
        }
    }
    assertTrue(false);
}
 
Example 7
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 8
Source File: TextUtils.java    From PowerFileExplorer 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>.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 */
public static void copySpansFrom(Spanned source, int start, int end,
                                 Class<?> kind,
                                 Spannable dest, int destoff) {
    if (kind == null) {
        kind = Object.class;
    }

    Object[] spans = source.getSpans(start, end, kind);

    for (int i = 0; i < spans.length; i++) {
        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);
        int fl = source.getSpanFlags(spans[i]);

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

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
                     fl);
    }
}
 
Example 9
Source File: DiscussionTextUtils.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Renders various HTML elements and plain hyperlinks in the given HTML to clickable items
 * while applying it on the given {@link TextView}.
 *
 * @param textView The {@link TextView} which will render the given HTML.
 * @param html     The HTML to render.
 */
public static void renderHtml(@NonNull TextView textView, @NonNull String html) {
    Spanned spannedHtml = DiscussionTextUtils.parseHtml(html);
    URLSpan[] urlSpans = spannedHtml.getSpans(0, spannedHtml.length(), URLSpan.class);
    textView.setAutoLinkMask(Linkify.ALL);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannedHtml);

    SpannableString viewText = (SpannableString) textView.getText();
    for (final URLSpan spanObj : urlSpans) {
        final int start = spannedHtml.getSpanStart(spanObj);
        final int end = spannedHtml.getSpanEnd(spanObj);
        final int flags = spannedHtml.getSpanFlags(spanObj);
        viewText.setSpan(spanObj, start, end, flags);
    }
}
 
Example 10
Source File: EditableSecureBuffer.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasNonExclusiveExclusiveSpanAt(CharSequence text, int offset) {
    if (text instanceof Spanned) {
        Spanned spanned = (Spanned) text;
        Object[] spans = spanned.getSpans(offset, offset, Object.class);
        final int length = spans.length;
        for (int i = 0; i < length; i++) {
            Object span = spans[i];
            int flags = spanned.getSpanFlags(span);
            if (flags != Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) return true;
        }
    }
    return false;
}
 
Example 11
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 12
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 13
Source File: SpannableStringBuilder.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Create a new SpannableStringBuilder containing a copy of the
 * specified slice of the specified text, including its spans if any.
 */
public SpannableStringBuilder(CharSequence text, int start, int end) {
    int srclen = end - start;

    int len = ArrayUtils.idealCharArraySize(srclen + 1);
    mText = new char[len];
    mGapStart = srclen;
    mGapLength = len - srclen;

    TextUtils.getChars(text, start, end, mText, 0);

    mSpanCount = 0;
    int alloc = ArrayUtils.idealIntArraySize(0);
    mSpans = new Object[alloc];
    mSpanStarts = new int[alloc];
    mSpanEnds = new int[alloc];
    mSpanFlags = new int[alloc];

    if (text instanceof Spanned) {
        Spanned sp = (Spanned) text;
        Object[] spans = sp.getSpans(start, end, Object.class);

        for (int i = 0; i < spans.length; i++) {
            if (spans[i] instanceof NoCopySpan) {
                continue;
            }

            int st = sp.getSpanStart(spans[i]) - start;
            int en = sp.getSpanEnd(spans[i]) - start;
            int fl = sp.getSpanFlags(spans[i]);

            if (st < 0)
                st = 0;
            if (st > end - start)
                st = end - start;

            if (en < 0)
                en = 0;
            if (en > end - start)
                en = end - start;

            if ( st <= en ) {
                setSpan(spans[i], st, en, fl);
            }
        }
    }
}
 
Example 14
Source File: MongolEditText.java    From mongol-library with MIT License 4 votes vote down vote up
private boolean isNonIntermediateSelectionSpan(final Spanned text, final Object span) {
    return (Selection.SELECTION_START == span || Selection.SELECTION_END == span)
            && (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0;
}
 
Example 15
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 16
Source File: SpannableStringBuilder.java    From JotaTextEditor with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new SpannableStringBuilder containing a copy of the
 * specified slice of the specified text, including its spans if any.
 */
public SpannableStringBuilder(CharSequence text, int start, int end) {
    int srclen = end - start;

    int len = ArrayUtils.idealCharArraySize(srclen + 1);
    mText = new char[len];
    mGapStart = srclen;
    mGapLength = len - srclen;

    TextUtils.getChars(text, start, end, mText, 0);

    mSpanCount = 0;
    int alloc = ArrayUtils.idealIntArraySize(0);
    mSpans = new Object[alloc];
    mSpanStarts = new int[alloc];
    mSpanEnds = new int[alloc];
    mSpanFlags = new int[alloc];

    if (text instanceof Spanned) {
        Spanned sp = (Spanned) text;
        Object[] spans = sp.getSpans(start, end, Object.class);

        for (int i = 0; i < spans.length; i++) {
            if (spans[i] instanceof NoCopySpan) {
                continue;
            }

            int st = sp.getSpanStart(spans[i]) - start;
            int en = sp.getSpanEnd(spans[i]) - start;
            int fl = sp.getSpanFlags(spans[i]);

            if (st < 0)
                st = 0;
            if (st > end - start)
                st = end - start;

            if (en < 0)
                en = 0;
            if (en > end - start)
                en = end - start;

            if ( st <= en ) {
                setSpan(spans[i], st, en, fl);
            }
        }
    }
}
 
Example 17
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 18
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 19
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 20
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++;
    }
}