Search in sources :

Example 41 with StrikethroughSpan

use of android.text.style.StrikethroughSpan in project Android-Rich-text-Editor by chinalwb.

the class AREditText method onSelectionChanged.

/*
	 * ----------------------------------------- * Rich Text Style Area
	 * -----------------------------------------
	 */
@Override
public void onSelectionChanged(int selStart, int selEnd) {
    if (sToolbar == null) {
        return;
    }
    Util.log(" on Selection changed == " + this);
    boolean boldExists = false;
    boolean italicsExists = false;
    boolean underlinedExists = false;
    boolean striketrhoughExists = false;
    boolean backgroundColorExists = false;
    boolean quoteExists = false;
    // 
    // Two cases:
    // 1. Selection is just a pure cursor
    // 2. Selection is a range
    Editable editable = this.getEditableText();
    if (selStart > 0 && selStart == selEnd) {
        CharacterStyle[] styleSpans = editable.getSpans(selStart - 1, selStart, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    boldExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    italicsExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                // TODO
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                underlinedExists = true;
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                striketrhoughExists = true;
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                backgroundColorExists = true;
            }
        }
        QuoteSpan[] quoteSpans = editable.getSpans(selStart - 1, selStart, QuoteSpan.class);
        if (quoteSpans != null && quoteSpans.length > 0) {
            quoteExists = true;
        }
    // To check Quote!!
    } else {
        // 
        // Selection is a range
        CharacterStyle[] styleSpans = editable.getSpans(selStart, selEnd, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        boldExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                        boldExists = true;
                    }
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    underlinedExists = true;
                }
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    striketrhoughExists = true;
                }
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    backgroundColorExists = true;
                }
            }
        }
    }
    QuoteSpan[] quoteSpans = editable.getSpans(selStart, selEnd, QuoteSpan.class);
    if (quoteSpans != null && quoteSpans.length > 0) {
        if (editable.getSpanStart(quoteSpans[0]) <= selStart && editable.getSpanEnd(quoteSpans[0]) >= selEnd) {
            quoteExists = true;
        }
    }
    // 
    // Set style checked status
    ARE_Helper.updateCheckStatus(sToolbar.getBoldStyle(), boldExists);
    ARE_Helper.updateCheckStatus(sToolbar.getItalicStyle(), italicsExists);
    ARE_Helper.updateCheckStatus(sToolbar.getUnderlineStyle(), underlinedExists);
    ARE_Helper.updateCheckStatus(sToolbar.getStrikethroughStyle(), striketrhoughExists);
    ARE_Helper.updateCheckStatus(sToolbar.getBackgroundColoStyle(), backgroundColorExists);
    ARE_Helper.updateCheckStatus(sToolbar.getQuoteStyle(), quoteExists);
}
Also used : AreUnderlineSpan(com.chinalwb.are.spans.AreUnderlineSpan) StyleSpan(android.text.style.StyleSpan) Editable(android.text.Editable) QuoteSpan(android.text.style.QuoteSpan) CharacterStyle(android.text.style.CharacterStyle) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 42 with StrikethroughSpan

use of android.text.style.StrikethroughSpan in project ExoPlayer by google.

the class CueSerializationTest method serializingBitmapCueAndCueWithAndroidSpans.

@Test
public void serializingBitmapCueAndCueWithAndroidSpans() {
    CueEncoder encoder = new CueEncoder();
    CueDecoder decoder = new CueDecoder();
    Spannable spannable = SpannableString.valueOf("text text");
    spannable.setSpan(new StrikethroughSpan(), 0, "text".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, "text text".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new StyleSpan(Typeface.ITALIC), 0, "text text".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new UnderlineSpan(), "text ".length(), "text text".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Cue textCue = new Cue.Builder().setText(spannable).build();
    Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    Cue bitmapCue = new Cue.Builder().setBitmap(bitmap).build();
    // encoding and decoding
    byte[] encodedCues = encoder.encode(ImmutableList.of(textCue, bitmapCue));
    List<Cue> cuesAfterDecoding = decoder.decode(encodedCues);
    assertThat(cuesAfterDecoding).hasSize(2);
    Cue textCueAfterDecoding = cuesAfterDecoding.get(0);
    Cue bitmapCueAfterDecoding = cuesAfterDecoding.get(1);
    assertThat(textCueAfterDecoding.text.toString()).isEqualTo(textCue.text.toString());
    SpannedSubject.assertThat((Spanned) textCueAfterDecoding.text).hasStrikethroughSpanBetween(0, "text".length());
    SpannedSubject.assertThat((Spanned) textCueAfterDecoding.text).hasBoldSpanBetween(0, "text text".length());
    SpannedSubject.assertThat((Spanned) textCueAfterDecoding.text).hasItalicSpanBetween(0, "text text".length());
    SpannedSubject.assertThat((Spanned) textCueAfterDecoding.text).hasUnderlineSpanBetween("text ".length(), "text text".length());
    assertThat(bitmapCueAfterDecoding.bitmap.sameAs(bitmap)).isTrue();
}
Also used : Bitmap(android.graphics.Bitmap) StyleSpan(android.text.style.StyleSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable) UnderlineSpan(android.text.style.UnderlineSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) Test(org.junit.Test)

Example 43 with StrikethroughSpan

use of android.text.style.StrikethroughSpan in project ExoPlayer by google.

the class UtilTest method truncateAscii_preservesStylingSpans.

@Test
public void truncateAscii_preservesStylingSpans() {
    SpannableString input = new SpannableString("a short string");
    input.setSpan(new UnderlineSpan(), 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    input.setSpan(new StrikethroughSpan(), 4, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    CharSequence result = Util.truncateAscii(input, 7);
    assertThat(result).isInstanceOf(SpannableString.class);
    assertThat(result.toString()).isEqualTo("a short");
    // TODO(internal b/161804035): Use SpannedSubject when it's available in a dependency we can use
    // from here.
    Spanned spannedResult = (Spanned) result;
    Object[] spans = spannedResult.getSpans(0, result.length(), Object.class);
    assertThat(spans).hasLength(2);
    assertThat(spans[0]).isInstanceOf(UnderlineSpan.class);
    assertThat(spannedResult.getSpanStart(spans[0])).isEqualTo(0);
    assertThat(spannedResult.getSpanEnd(spans[0])).isEqualTo(7);
    assertThat(spannedResult.getSpanFlags(spans[0])).isEqualTo(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    assertThat(spans[1]).isInstanceOf(StrikethroughSpan.class);
    assertThat(spannedResult.getSpanStart(spans[1])).isEqualTo(4);
    assertThat(spannedResult.getSpanEnd(spans[1])).isEqualTo(7);
    assertThat(spannedResult.getSpanFlags(spans[1])).isEqualTo(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
Also used : SpannableString(android.text.SpannableString) Spanned(android.text.Spanned) UnderlineSpan(android.text.style.UnderlineSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) Test(org.junit.Test)

Example 44 with StrikethroughSpan

use of android.text.style.StrikethroughSpan in project ExoPlayer by google.

the class SpannedSubjectTest method strikethroughSpan_success.

@Test
public void strikethroughSpan_success() {
    SpannableString spannable = createSpannable(new StrikethroughSpan(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    assertThat(spannable).hasStrikethroughSpanBetween(SPAN_START, SPAN_END).withFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
Also used : SpannableString(android.text.SpannableString) StrikethroughSpan(android.text.style.StrikethroughSpan) Test(org.junit.Test)

Example 45 with StrikethroughSpan

use of android.text.style.StrikethroughSpan in project ExoPlayer by google.

the class SsaDecoder method createCue.

private static Cue createCue(String text, @Nullable SsaStyle style, SsaStyle.Overrides styleOverrides, float screenWidth, float screenHeight) {
    SpannableString spannableText = new SpannableString(text);
    Cue.Builder cue = new Cue.Builder().setText(spannableText);
    if (style != null) {
        if (style.primaryColor != null) {
            spannableText.setSpan(new ForegroundColorSpan(style.primaryColor), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.fontSize != Cue.DIMEN_UNSET && screenHeight != Cue.DIMEN_UNSET) {
            cue.setTextSize(style.fontSize / screenHeight, Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING);
        }
        if (style.bold && style.italic) {
            spannableText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (style.bold) {
            spannableText.setSpan(new StyleSpan(Typeface.BOLD), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (style.italic) {
            spannableText.setSpan(new StyleSpan(Typeface.ITALIC), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.underline) {
            spannableText.setSpan(new UnderlineSpan(), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (style.strikeout) {
            spannableText.setSpan(new StrikethroughSpan(), /* start= */
            0, /* end= */
            spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    @SsaStyle.SsaAlignment int alignment;
    if (styleOverrides.alignment != SsaStyle.SSA_ALIGNMENT_UNKNOWN) {
        alignment = styleOverrides.alignment;
    } else if (style != null) {
        alignment = style.alignment;
    } else {
        alignment = SsaStyle.SSA_ALIGNMENT_UNKNOWN;
    }
    cue.setTextAlignment(toTextAlignment(alignment)).setPositionAnchor(toPositionAnchor(alignment)).setLineAnchor(toLineAnchor(alignment));
    if (styleOverrides.position != null && screenHeight != Cue.DIMEN_UNSET && screenWidth != Cue.DIMEN_UNSET) {
        cue.setPosition(styleOverrides.position.x / screenWidth);
        cue.setLine(styleOverrides.position.y / screenHeight, LINE_TYPE_FRACTION);
    } else {
        // TODO: Read the MarginL, MarginR and MarginV values from the Style & Dialogue lines.
        cue.setPosition(computeDefaultLineOrPosition(cue.getPositionAnchor()));
        cue.setLine(computeDefaultLineOrPosition(cue.getLineAnchor()), LINE_TYPE_FRACTION);
    }
    return cue.build();
}
Also used : SpannableString(android.text.SpannableString) Cue(com.google.android.exoplayer2.text.Cue) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) UnderlineSpan(android.text.style.UnderlineSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Aggregations

StrikethroughSpan (android.text.style.StrikethroughSpan)59 ForegroundColorSpan (android.text.style.ForegroundColorSpan)27 StyleSpan (android.text.style.StyleSpan)27 TypefaceSpan (android.text.style.TypefaceSpan)24 BackgroundColorSpan (android.text.style.BackgroundColorSpan)23 UnderlineSpan (android.text.style.UnderlineSpan)20 RelativeSizeSpan (android.text.style.RelativeSizeSpan)19 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)16 SpannableString (android.text.SpannableString)15 SubscriptSpan (android.text.style.SubscriptSpan)15 SuperscriptSpan (android.text.style.SuperscriptSpan)15 URLSpan (android.text.style.URLSpan)14 LeadingMarginSpan (android.text.style.LeadingMarginSpan)11 CharacterStyle (android.text.style.CharacterStyle)9 ImageSpan (android.text.style.ImageSpan)9 SpannableStringBuilder (android.text.SpannableStringBuilder)8 TextPaint (android.text.TextPaint)7 QuoteSpan (android.text.style.QuoteSpan)7 Application (android.app.Application)6 AlignmentSpan (android.text.style.AlignmentSpan)6