out.append("<font color =\"#"); String color = Integer.toHexString(((ForegroundColorSpan) style[j]).getForegroundColor() + 0x01000000); while (color.length() < 6) { color = "0" + color;
private void assertForeground(Spannable spannable, int foregroundColor) { ForegroundColorSpan[] foregroundColorSpans = spannable.getSpans(0, spannable.length(), ForegroundColorSpan.class); assertThat(foregroundColorSpans[foregroundColorSpans.length - 1].getForegroundColor()) .isEqualTo(foregroundColor); }
@Test public void testDecodeWithMultipleStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_MULTIPLE_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("Line 2\nLine 3"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); StyleSpan styleSpan = findSpan(text, 0, 5, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.ITALIC); findSpan(text, 7, 12, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 5, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); colorSpan = findSpan(text, 7, 12, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithOtherExtension() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_OTHER_EXTENSION); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(2); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testDecodeWithStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testInitializationAllDefaultsDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(RuntimeEnvironment.application, INITIALIZATION_ALL_DEFAULTS); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testInitializationDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(RuntimeEnvironment.application, INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(5); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED); colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1f); }
@Test public void testInitializationDecodeWithTbox() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(RuntimeEnvironment.application, INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, SAMPLE_WITH_TBOX); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1875f); }
assertThat( text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class)[0] .getForegroundColor()) .isEqualTo(0xFFEE82EE); assertThat(text.getSpans(/* start= */ 30, text.length(), TypefaceSpan.class)).hasLength(1);
@Override int getColorForSpan(ForegroundColorSpan span) { return(span.getForegroundColor()); }
@Override public String getStartTagForSpan(ForegroundColorSpan span) { return("<font color=\"#" + String.format("%06x", 0xFFFFFF & span.getForegroundColor()) + "\">"); }
public static JsonObject spanToJson(Object span) { JsonObject ret = new JsonObject(); if (span instanceof ForegroundColorSpan) { ret.addProperty("type", SPAN_TYPE_FOREGROUND); ret.addProperty("color", ((ForegroundColorSpan) span).getForegroundColor()); } else if (span instanceof BackgroundColorSpan) { ret.addProperty("type", SPAN_TYPE_BACKGROUND); ret.addProperty("color", ((BackgroundColorSpan) span).getBackgroundColor()); } else if (span instanceof StyleSpan) { ret.addProperty("type", SPAN_TYPE_STYLE); ret.addProperty("style", ((StyleSpan) span).getStyle()); } else { return null; } return ret; }
public static boolean areSpansEqual(Object span, Object span2) { if (span instanceof ForegroundColorSpan && span2 instanceof ForegroundColorSpan) return ((ForegroundColorSpan) span).getForegroundColor() == ((ForegroundColorSpan) span2).getForegroundColor(); if (span instanceof BackgroundColorSpan && span2 instanceof BackgroundColorSpan) return ((BackgroundColorSpan) span).getBackgroundColor() == ((BackgroundColorSpan) span2).getBackgroundColor(); if (span instanceof StyleSpan && span2 instanceof StyleSpan) return ((StyleSpan) span).getStyle() == ((StyleSpan) span2).getStyle(); return true; }
public static Object cloneSpan(Object span) { if (span instanceof ForegroundColorSpan) return new ForegroundColorSpan(((ForegroundColorSpan) span).getForegroundColor()); if (span instanceof BackgroundColorSpan) return new BackgroundColorSpan(((BackgroundColorSpan) span).getBackgroundColor()); if (span instanceof StyleSpan) return new StyleSpan(((StyleSpan) span).getStyle()); return null; }
/** * Get Foreground color of View. * * @return The background color of View. */ public int getForegroundColor(int pos) { if (pos < 0 || pos > getText().length()) { return DEFAULT_FOREGROUND_COLOR; } else { ForegroundColorSpan[] spans = getText().getSpans(pos, pos, ForegroundColorSpan.class); if (spans.length > 0) { return spans[0].getForegroundColor(); } else { return DEFAULT_FOREGROUND_COLOR; } } }
private CharSequence fixSpanColor(CharSequence text) { if (text instanceof Spanned) { final SpannableString s = new SpannableString(text); final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class); for (final ForegroundColorSpan oldSpan : spans) { final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000); s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan)); s.removeSpan(oldSpan); } return s; } else { return text; } }
private static CharSequence fixSpanColor(CharSequence text) { if (text instanceof Spanned) { final SpannableString s = new SpannableString(text); final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class); for (final ForegroundColorSpan oldSpan : spans) { final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000); s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan)); s.removeSpan(oldSpan); } return s; } else { return text; } } }
private void renewColor() { HorizontalLineSpan parent = getParentSpan(); Spannable text = mSpannable; int start = text.getSpanStart(parent); int end = text.getSpanEnd(parent); ForegroundColorSpan[] spans = text.getSpans(start, end, ForegroundColorSpan.class); if (DBG_HL) { Log.d(LOG_TAG, "--- renewColor:" + spans.length); } if (spans.length > 0) { renewColor(spans[spans.length - 1].getForegroundColor()); } }
if (o instanceof ForegroundColorSpan) { if (spanStart > fgColorS) { fgColor = ((ForegroundColorSpan) o).getForegroundColor(); fgColorS = spanStart;
private int getColorForText(final TextView textView, final String textToFind) { final Spannable allText = (Spannable) textView.getText(); final int textToFindIndex = allText.toString().indexOf(textToFind); final Spannable[] resultArray = new Spannable[1]; textView.post(new Runnable() { @Override public void run() { // subSequence does something on the ui thread resultArray[0] = (Spannable) allText.subSequence(textToFindIndex, textToFindIndex + textToFind.length()); } }); solo.sleep(200); final Spannable textToFindSpan = resultArray[0]; assertEquals(textToFind, textToFindSpan.toString()); final ForegroundColorSpan[] spans = textToFindSpan.getSpans(0, textToFindSpan.length(), ForegroundColorSpan.class); assertEquals(1, spans.length); return spans[0].getForegroundColor(); }