Java Code Examples for android.text.style.AlignmentSpan

The following examples show how to use android.text.style.AlignmentSpan. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: android_9.0.0_r45   Source File: Layout.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source Project: Markwon   Source File: HtmlActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Object getSpans(
        @NonNull MarkwonConfiguration configuration,
        @NonNull RenderProps renderProps,
        @NonNull HtmlTag tag) {

    final Layout.Alignment alignment;

    // html attribute without value, <align center></align>
    if (tag.attributes().containsKey("center")) {
        alignment = Layout.Alignment.ALIGN_CENTER;
    } else if (tag.attributes().containsKey("end")) {
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
    } else {
        // empty value or any other will make regular alignment
        alignment = Layout.Alignment.ALIGN_NORMAL;
    }

    return new AlignmentSpan.Standard(alignment);
}
 
Example 3
Source Project: Ruisi   Source File: SpanConverter.java    License: Apache License 2.0 6 votes vote down vote up
private void handleParagraph(int start, HtmlNode.HtmlAttr attr) {
    if (attr == null) {
        return;
    }
    setSpan(start, new StyleSpan(attr));

    Layout.Alignment align;
    if (attr.textAlign == HtmlNode.ALIGN_LEFT) {
        align = Layout.Alignment.ALIGN_NORMAL;
    } else if (attr.textAlign == HtmlNode.ALIGN_RIGHT) {
        align = Layout.Alignment.ALIGN_OPPOSITE;
    } else if (attr.textAlign == HtmlNode.ALIGN_CENTER) {
        align = Layout.Alignment.ALIGN_CENTER;
    } else {
        align = null;
    }

    if (align != null) {
        setSpan(start, position, new AlignmentSpan.Standard(align));
    }
}
 
Example 4
Source Project: android_9.0.0_r45   Source File: Html.java    License: Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 5
Source Project: android_9.0.0_r45   Source File: Html.java    License: Apache License 2.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example 6
Source Project: MHViewer   Source File: Html.java    License: Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 7
Source Project: tysq-android   Source File: Html.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example 8
Source Project: FairEmail   Source File: HtmlEx.java    License: GNU General Public License v3.0 5 votes vote down vote up
private /* static */ void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 9
/**
 * Show a toast
 */
public static void showToast(String content, Context context) {
    Spannable centeredText = new SpannableString(content);
    centeredText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
            0, content.length() - 1,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    Toast t = Toast.makeText(context,
            centeredText,
            Toast.LENGTH_SHORT);
    t.setGravity(Gravity.BOTTOM, 0, (int) UIUtil.convertDpToPixel(100, context));
    t.show();
}
 
Example 10
Source Project: RichEditor   Source File: MarkDownParser.java    License: Apache License 2.0 5 votes vote down vote up
private String paragraphToMarkDown(List<Object> mSpans, StringBuilder source) {
    if (mSpans == null || mSpans.size() == 0) {
        return source.toString();
    }
    String str = source.toString();
    for (Object obj : mSpans) {
        if (obj instanceof ReferSpan) {
            str = formater.formatRefer(str);
        } else if (obj instanceof AlignmentSpan) {
            //markdown是不支持居中的!!!,后期考虑注释掉
            // str = formater.formatCenter(str);
        } else if (obj instanceof AbsoluteSizeSpan) {
            AbsoluteSizeSpan sizeSpan = (AbsoluteSizeSpan) obj;
            switch (sizeSpan.getSize()) {
                case Const.T1_SIZE:
                    str = formater.formatH1(str);
                    break;
                case Const.T2_SIZE:
                    str = formater.formatH2(str);
                    break;
                case Const.T3_SIZE:
                    str = formater.formatH3(str);
                    break;
                case Const.T4_SIZE:
                    str = formater.formatH4(str);
                    break;
            }
        }
    }
    return str;
}
 
Example 11
Source Project: SpanEZ   Source File: SpanEZTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void align_start_should_add_only_one_span() {
    spanBuilder.alignStart(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example 12
Source Project: SpanEZ   Source File: SpanEZTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void align_center_should_add_only_one_span() {
    spanBuilder.alignCenter(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example 13
Source Project: SpanEZ   Source File: SpanEZTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void align_end_should_add_only_one_span() {
    spanBuilder.alignEnd(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example 14
Source Project: ucar-weex-core   Source File: WXTextDomObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end, int spanFlag) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE || mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end, new TextDecorationSpan(mTextDecoration), spanFlag));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor), spanFlag));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize), spanFlag));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily),
                                   spanFlag));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment), spanFlag));
    if (mLineHeight != UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight), spanFlag));
    }
  }
  return ops;
}
 
Example 15
Source Project: HtmlCompat   Source File: HtmlCompat.java    License: Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(Context context, StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] styles = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;
        for (ParagraphStyle style : styles) {
            if (style instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style).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(context, out, text, i, next, option);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 16
Source Project: HtmlCompat   Source File: HtmlCompat.java    License: 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 17
Source Project: HtmlCompat   Source File: HtmlToSpannedConverter.java    License: Apache License 2.0 5 votes vote down vote up
private void endBlockElement(String tag, Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }
    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(tag, text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example 18
Source Project: weex-uikit   Source File: WXTextDomObject.java    License: MIT License 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end, int spanFlag) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE) {
      ops.add(new SetSpanOperation(start, end,
                                   new UnderlineSpan(), spanFlag));
    }
    if (mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end,
                                   new StrikethroughSpan(), spanFlag));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor), spanFlag));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize), spanFlag));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily),
                                   spanFlag));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment), spanFlag));
    if (mLineHeight != UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight), spanFlag));
    }
  }
  return ops;
}
 
Example 19
Source Project: 600SeriesAndroidUploader   Source File: MainActivity.java    License: MIT License 5 votes vote down vote up
private void chartViewAdjusted() {
    chartChangeTimestamp = System.currentTimeMillis();

    long end = timeLastSGV == 0 ? chartChangeTimestamp - chartTimeOffset : timeLastSGV - chartTimeOffset;
    long start = end - chartZoom * 60 * 60000L;

    String t = String.format(getString(R.string.main_screen__value_hour_chart), chartZoom);

    String m = String.format("\n%s %s",
            FormatKit.getInstance().formatAsMonthName(start),
            FormatKit.getInstance().formatAsDay(start)
    );

    String d = String.format("\n%s - %s",
            FormatKit.getInstance().formatAsDayClock(start),
            FormatKit.getInstance().formatAsDayClock(end)
    );

    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(t);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - t.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new StyleSpan(Typeface.BOLD), ssb.length() - t.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ssb.append(m);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - m.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new RelativeSizeSpan(0.75f), ssb.length() - m.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ssb.append(d);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - d.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new RelativeSizeSpan(0.85f), ssb.length() - d.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    toast = serveToast(ssb, toast, findViewById(R.id.view_sgv));

    refreshDisplayChart();
}
 
Example 20
Source Project: ForPDA   Source File: Html.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    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);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 21
Source Project: ForPDA   Source File: Html.java    License: 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 22
Source Project: ForPDA   Source File: Html.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }
    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example 23
Source Project: text-decorator   Source File: TextDecorator.java    License: Apache License 2.0 5 votes vote down vote up
public TextDecorator alignText(final Layout.Alignment alignment, final String... texts) {
  int index;

  for (String text : texts) {
    if (content.contains(text)) {
      index = content.indexOf(text);
      decoratedContent.setSpan(new AlignmentSpan.Standard(alignment), index, index + text.length(), flags);
    }
  }

  return this;
}
 
Example 24
Source Project: Tehreer-Android   Source File: FrameResolver.java    License: Apache License 2.0 5 votes vote down vote up
float computeFlushFactor() {
    Layout.Alignment alignment = null;

    // Get the top most alignment.
    for (int i = paragraphSpans.length - 1; i >= 0; i--) {
        if (paragraphSpans[i] instanceof AlignmentSpan) {
            alignment = ((AlignmentSpan) paragraphSpans[i]).getAlignment();
            break;
        }
    }

    return getFlushFactor(alignment, baseLevel);
}
 
Example 25
Source Project: weex   Source File: WXTextDomObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE) {
      ops.add(new SetSpanOperation(start, end,
                                   new UnderlineSpan()));
    }
    if (mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end,
                                   new StrikethroughSpan()));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor)));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize)));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily)));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment)));
    if(mLineHeight !=UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight)));
    }
  }
  return ops;
}
 
Example 26
Source Project: Ruisi   Source File: SpanConverter.java    License: Apache License 2.0 5 votes vote down vote up
private void handleBlockTag(boolean isStart, int type, int start, HtmlNode.HtmlAttr attr) {
    if (position <= 0) {
        return;
    }
    if (spannedBuilder.charAt(position - 1) != '\n') {
        spannedBuilder.append('\n');
        position++;
    }

    //结束block 标签
    if (!isStart && attr != null) {
        Layout.Alignment align;
        if (attr.align == HtmlNode.ALIGN_LEFT) {
            align = Layout.Alignment.ALIGN_NORMAL;
        } else if (attr.align == HtmlNode.ALIGN_RIGHT) {
            align = Layout.Alignment.ALIGN_OPPOSITE;
        } else if (attr.align == HtmlNode.ALIGN_CENTER) {
            align = Layout.Alignment.ALIGN_CENTER;
        } else {
            align = null;
        }

        if (align != null) {
            setSpan(start, position, new AlignmentSpan.Standard(align));
        }
    }
}
 
Example 27
Source Project: nono-android   Source File: MyHtml.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 28
Source Project: Nimingban   Source File: Html.java    License: Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 29
Source Project: RichEditText   Source File: EmailHtmlUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text ) {
	int len = text.length();

	int next;
	for (int i = 0; i < text.length(); 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 " + elements + ">");
		}

		withinDiv(out, text, i, next);

		if (needDiv) {
			out.append("</div>");
		}
	}
}
 
Example 30
Source Project: advanced-textview   Source File: AlignmentSpanActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void appendText(CharSequence text, Layout.Alignment align) {
  if (text == null || text.toString().trim().length() == 0) {
    return;
  }

  AlignmentSpan span = new AlignmentSpan.Standard(align);
  SpannableString spannableString = new SpannableString(text);
  spannableString.setSpan(span, 0, text.length(), 0);

  if (textView.length() > 0) {
    textView.append("\n\n");
  }
  textView.append(spannableString);
}
 
Example 31
Source Project: openScale   Source File: ChartMarkerView.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void refreshContent(Entry e, Highlight highlight) {
    Object[] extraData = (Object[])e.getData();
    ScaleMeasurement measurement = (ScaleMeasurement)extraData[0];
    ScaleMeasurement prevMeasurement = (ScaleMeasurement)extraData[1];
    FloatMeasurementView measurementView = (FloatMeasurementView)extraData[2];

    SpannableStringBuilder markerText = new SpannableStringBuilder();

    if (measurement != null) {
        measurementView.loadFrom(measurement, prevMeasurement);
        DateFormat dateFormat = DateFormat.getDateInstance();
        markerText.append(dateFormat.format(measurement.getDateTime()));
        markerText.setSpan(new RelativeSizeSpan(0.8f), 0, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        markerText.append("\n");

        if (measurement.isAverageValue()) {
            markerText.append("Ø ");
        }
    }

    markerText.append(measurementView.getValueAsString(true));

    if (prevMeasurement != null) {
        markerText.append("\n");
        int textPosAfterSymbol = markerText.length() + 1;

        measurementView.appendDiffValue(markerText, false);

        // set color diff value to text color
        if (markerText.length() > textPosAfterSymbol) {
            markerText.setSpan(new ForegroundColorSpan(ColorUtil.COLOR_WHITE), textPosAfterSymbol, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    markerText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),0, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    markerTextField.setText(markerText);

    super.refreshContent(e, highlight);
}
 
Example 32
Source Project: EhViewer   Source File: Html.java    License: Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 33
Source Project: v2ex   Source File: HtmlTagHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
    if (opening) {
        // opening tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "opening, output: " + output.toString());
        }

        if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
            mListParents.add(tag);
            mListItemCount = 0;
        } else if (tag.equalsIgnoreCase("code")) {
            start(output, new Code());
        } else if (tag.equalsIgnoreCase("center")) {
            start(output, new Center());
        }
    } else {
        // closing tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "closing, output: " + output.toString());
        }

        if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
            mListParents.remove(tag);
            mListItemCount = 0;
        } else if (tag.equalsIgnoreCase("li")) {
            handleListTag(output);
        } else if (tag.equalsIgnoreCase("code")) {
            end(output, Code.class, new TypefaceSpan("monospace"), false);
        } else if (tag.equalsIgnoreCase("center")) {
            end(output, Center.class, new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), true);
        }
    }
}
 
Example 34
Source Project: Mupdf   Source File: AKHtml.java    License: Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 35
Source Project: Mupdf   Source File: AKHtml.java    License: Apache License 2.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example 36
Source Project: MediaSDK   Source File: WebvttCueParser.java    License: Apache License 2.0 4 votes vote down vote up
private static void applyStyleToText(SpannableStringBuilder spannedText, WebvttCssStyle style,
    int start, int end) {
  if (style == null) {
    return;
  }
  if (style.getStyle() != WebvttCssStyle.UNSPECIFIED) {
    spannedText.setSpan(new StyleSpan(style.getStyle()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isLinethrough()) {
    spannedText.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isUnderline()) {
    spannedText.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasFontColor()) {
    spannedText.setSpan(new ForegroundColorSpan(style.getFontColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasBackgroundColor()) {
    spannedText.setSpan(new BackgroundColorSpan(style.getBackgroundColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.getFontFamily() != null) {
    spannedText.setSpan(new TypefaceSpan(style.getFontFamily()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  Layout.Alignment textAlign = style.getTextAlign();
  if (textAlign != null) {
    spannedText.setSpan(
        new AlignmentSpan.Standard(textAlign), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  switch (style.getFontSizeUnit()) {
    case WebvttCssStyle.FONT_SIZE_UNIT_PIXEL:
      spannedText.setSpan(new AbsoluteSizeSpan((int) style.getFontSize(), true), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.FONT_SIZE_UNIT_EM:
      spannedText.setSpan(new RelativeSizeSpan(style.getFontSize()), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.FONT_SIZE_UNIT_PERCENT:
      spannedText.setSpan(new RelativeSizeSpan(style.getFontSize() / 100), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.UNSPECIFIED:
      // Do nothing.
      break;
  }
}
 
Example 37
Source Project: MediaSDK   Source File: TtmlRenderUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static void applyStylesToSpan(SpannableStringBuilder builder,
    int start, int end, TtmlStyle style) {

  if (style.getStyle() != TtmlStyle.UNSPECIFIED) {
    builder.setSpan(new StyleSpan(style.getStyle()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isLinethrough()) {
    builder.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isUnderline()) {
    builder.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasFontColor()) {
    builder.setSpan(new ForegroundColorSpan(style.getFontColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasBackgroundColor()) {
    builder.setSpan(new BackgroundColorSpan(style.getBackgroundColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.getFontFamily() != null) {
    builder.setSpan(new TypefaceSpan(style.getFontFamily()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.getTextAlign() != null) {
    builder.setSpan(new AlignmentSpan.Standard(style.getTextAlign()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  switch (style.getFontSizeUnit()) {
    case TtmlStyle.FONT_SIZE_UNIT_PIXEL:
      builder.setSpan(new AbsoluteSizeSpan((int) style.getFontSize(), true), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case TtmlStyle.FONT_SIZE_UNIT_EM:
      builder.setSpan(new RelativeSizeSpan(style.getFontSize()), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case TtmlStyle.FONT_SIZE_UNIT_PERCENT:
      builder.setSpan(new RelativeSizeSpan(style.getFontSize() / 100), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case TtmlStyle.UNSPECIFIED:
      // Do nothing.
      break;
  }
}
 
Example 38
Source Project: star-zone-android   Source File: MultiSpanUtil.java    License: Apache License 2.0 4 votes vote down vote up
private void applySpan(SpannableStringBuilder spanBuilder, String keyWord, @NonNull ItemOption option, int index) {
    if (index <= -1 || spanBuilder == null || option == null || TextUtils.isEmpty(keyWord) || (index + keyWord.length() <= 0))
        return;
    //color
    if (option.getTextColor() != -1) {
        spanBuilder.setSpan(new ForegroundColorSpan(option.getTextColor()), index, index + keyWord
                .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    //texttype
    if (option.getTextType() != Typeface.DEFAULT) {
        spanBuilder.setSpan(new StyleSpan(option.getTextType().getStyle()), index, index + keyWord
                .length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    //textsize
    if (option.getTextSize() != -1) {
        spanBuilder.setSpan(new AbsoluteSizeSpan(option.getTextSize()), index, index + keyWord
                .length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    //underline
    if (option.isUnderLine()) {
        spanBuilder.setSpan(new UnderlineSpan(), index, index + keyWord
                .length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    //urlSpan
    if (option.isUrl()) {
        spanBuilder.setSpan(new UrlSpanEx(keyWord, option.urlColor, option.onUrlClickListener), index, index + keyWord
                .length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    //bgcolor
    if (option.getBgColor() != -1) {
        spanBuilder.setSpan(new BackgroundColorSpan(option.getBgColor()), index, index + keyWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    //gravity
    if (option.getTextGravity() != null) {
        spanBuilder.setSpan(new AlignmentSpan.Standard(option.getTextGravity()), index, index + keyWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

}
 
Example 39
Source Project: android_9.0.0_r45   Source File: Html.java    License: 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 40
private static void applyStyleToText(SpannableStringBuilder spannedText, WebvttCssStyle style,
    int start, int end) {
  if (style == null) {
    return;
  }
  if (style.getStyle() != WebvttCssStyle.UNSPECIFIED) {
    spannedText.setSpan(new StyleSpan(style.getStyle()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isLinethrough()) {
    spannedText.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.isUnderline()) {
    spannedText.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasFontColor()) {
    spannedText.setSpan(new ForegroundColorSpan(style.getFontColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.hasBackgroundColor()) {
    spannedText.setSpan(new BackgroundColorSpan(style.getBackgroundColor()), start, end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.getFontFamily() != null) {
    spannedText.setSpan(new TypefaceSpan(style.getFontFamily()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  if (style.getTextAlign() != null) {
    spannedText.setSpan(new AlignmentSpan.Standard(style.getTextAlign()), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  switch (style.getFontSizeUnit()) {
    case WebvttCssStyle.FONT_SIZE_UNIT_PIXEL:
      spannedText.setSpan(new AbsoluteSizeSpan((int) style.getFontSize(), true), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.FONT_SIZE_UNIT_EM:
      spannedText.setSpan(new RelativeSizeSpan(style.getFontSize()), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.FONT_SIZE_UNIT_PERCENT:
      spannedText.setSpan(new RelativeSizeSpan(style.getFontSize() / 100), start, end,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      break;
    case WebvttCssStyle.UNSPECIFIED:
      // Do nothing.
      break;
  }
}